Hi everyone,
I'm having a real problem getting casperjs to locate a DOM element that is loaded asynchronously after page load. The element's id is "dl_url", and I'm absolutely sure that the element is loaded via AJAX into the page a few seconds after initial page load.
Note: I am using casperjs version 1.1.0-DEV.
Here is my JS file:
function loadError()
{
casper.echo("error");
casper.exit();
}
var casper = require('casper').create({
pageSettings: {
loadImages: false,
userAgent: 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'
},
waitTimeout: 38000,
timeout: 40000,
stepTimeout: 38000,
verbose: true,
logLevel: "debug",
onLoadError: loadError
});
casper.start(casper.cli.args[0]);
casper.then(function() {
this.waitForSelector('#dl_url', function() {
this.echo('loaded');
var ua = this.evaluate(function() {
return document.getElementById('dl_url').getElementsByTagName('a')[0].href;
});
this.echo(ua);
},
function()
{
this.echo('not loaded');
});
});
casper.run(function() {
this.exit();
});
And here is the command and CLI output [some details have been changed to protect the innocent ;-)]:
root@ns5000918:~# casperjs --proxy=
00.000.000.000:8800 --proxy-type=none /home/xxxxx/public_html/phantom/getDlink2.js
http://www.mySite.com[info] [phantom] Starting...
[info] [phantom] Execution timeout set to 40000ms
[info] [phantom] Running suite: 3 steps
[debug] [phantom] opening url:
http://www.mySite.com, HTTP GET
[debug] [phantom] Navigation requested: url=
http://www.mySite.com, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "
http://www.mySite.com"
[debug] [phantom] Successfully injected Casper client-side utilities
[debug] [phantom] start page is loaded
[info] [phantom] Step anonymous 3/3
http://www.mySite.com (HTTP 0)
[info] [phantom] Step anonymous 3/3: done in 3133ms.
[info] [phantom] Step _step 4/4
http://www.mySite.com (HTTP 0)
[info] [phantom] Step _step 4/4: done in 3154ms.
[error] [phantom] Script timeout of 40000ms reached, exiting.
Script timeout of 40000ms reached, exiting.
From where I'm standing, it appears that casper.waitForSelector never finds the element with id "dl_url". And I can't for the life of me figure out why. I feel like I've tried everything, and I've poured over the casperjs api docs.
It should be noted that with plain, old phantomjs, I WAS able to find this element in the DOM using a custom waitFor function. It's only since I've switched to casperjs that I've been having this problem.
Can anyone help shed some light on this issue for me? Thanks very much in advance.
RJ