最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - injectJs with PhantomJs and CasperJs - Stack Overflow

programmeradmin5浏览0评论

I am trying to use CasperJS to scrape a website that has dynamic content and am failing so far. The website uses a couple of js libaries(e.g. Prototype) to use autoplete to create content. I am therefore trying to insert some values and trigger events to set off the content creation. Inserting and triggering events works fine, but no content is created.

I am new to CasperJS and found that it has problems with relative paths to the libaries. I suspect that my problem arises from the libaries not loading properly in my page environment. I therefore tried to use injectJs to inject them like this

// ... create casper and do some other stuff

casper.then(function() {

this.echo(this.page.injectJs(''));
this.echo(this.page.injectJs(''));

 });

Unfortunately this returns false for both of the functions. The path to the script is correct, where else could be the root of this problem?

Thank you very much for any help.

I am trying to use CasperJS to scrape a website that has dynamic content and am failing so far. The website uses a couple of js libaries(e.g. Prototype) to use autoplete to create content. I am therefore trying to insert some values and trigger events to set off the content creation. Inserting and triggering events works fine, but no content is created.

I am new to CasperJS and found that it has problems with relative paths to the libaries. I suspect that my problem arises from the libaries not loading properly in my page environment. I therefore tried to use injectJs to inject them like this

// ... create casper and do some other stuff

casper.then(function() {

this.echo(this.page.injectJs('http://www.my-website./path/to/js1'));
this.echo(this.page.injectJs('http://www.my-website./path/to/js2'));

 });

Unfortunately this returns false for both of the functions. The path to the script is correct, where else could be the root of this problem?

Thank you very much for any help.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jun 28, 2012 at 3:18 MarcoMarco 1,4921 gold badge16 silver badges29 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I do not believe CasperJS can handle HTTP requests to inject scripts. This is weird because they actually show this in their FAQ on including jQuery. However, that same FAQ says the following:

You can't inject scripts using the HTTP protocol, you actually have to use a relative/absolute filesystem path to the script resource.

Just to double check, I tried it myself using Mac OS X and your above code. The results of echo were false when using a file over http and true when it was local.

this is how I Inject Jquery in Casperjs script

casper.options.clientScripts = ["jquery-3.2.1.min.js"]

Jquery file should be on same directory where casperjs script exisit

发布评论

评论列表(0)

  1. 暂无评论