Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

injectJs with PhantomJs and CasperJs

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 autocomplete 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.com/path/to/js1'));
this.echo(this.page.injectJs('http://www.my-website.com/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.

like image 784
Marco Avatar asked Jun 28 '12 03:06

Marco


2 Answers

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.

like image 188
scottheckel Avatar answered Sep 20 '22 03:09

scottheckel


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

like image 31
Haisam Hameed Avatar answered Sep 21 '22 03:09

Haisam Hameed