我想刮一個網頁,其形式有很多下拉和表單中的值是相互依賴的。在很多時候,我需要代碼等待頁面刷新完成。例如,從列表中選擇一個選項後,代碼應該等到下一個列表根據此選擇填充。如果有人能夠提供指針會非常有幫助,因爲奇怪的是我的代碼只有在我給了這麼多不必要的日誌語句,而這又造成了一些延遲之後才起作用。任何改進代碼的建議都會非常有用。如何在使用casperjs時等待頁面加載?
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
userAgent: 'Mozilla/5.0 poi poi poi (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22',
pageSettings: {}
});
casper.start('http://www.abc.com', function() {
console.log("casper started");
this.fill('form[action="http://www.abc.com/forum/member.php"]', {
quick_username: "qwe",
quick_password: "qwe"
}, true);
this.capture('screen.png');
});
casper.thenOpen("http://www.abc.com/search/index.php").then(function() {
this.click('input[type="checkbox"][name="firstparam"]');
this.click('a#poi');
casper.evaluate(function() {
document.getElementsByName("status")[0].value = 1;
document.getElementsByName("state")[0].value = 1078;
changeState(); //This function is associated with the dropdown ie state
and the page reloads at this point. Only after complete refresh the code shoud execute! How can this be achieved?
return true;
});
this.echo('Inside the first thenOpen' + this.evaluate(function() {
return document.search.action;
}));
});
casper.then(function() {
this.capture("poi.png");
console.log('just before injecting jquery');
casper.page.injectJs('./jquery.js');
this.click('input[type="checkbox"][name="or"]');
this.evaluate(function() {
$('.boxline .filelist input:checkbox[value=18127]').attr("checked", true);
});
this.echo('Just before pressing the add college button' + this.evaluate(function() {
return document.search.action;
}));
this.capture('collegeticked.png');
if (this.exists('input[type="button"][name="niv"]')) {
this.echo('button is there');
} else {
this.echo('button is not there');
}
this.echo("Going to print return value");
this.click('input[type="button"][name="poi"]'); // This click again causes a page refresh. Code should wait at this point for completion.
this.echo('Immediately after pressing the add college btn getPresentState()' + this.evaluate(function() {
return getPresentState();
}));
this.echo('Immediately after pressing add colleg button' + this.evaluate(function() {
return document.search.action;
}));
this.capture('iu.png');
});
casper.then(function() {
console.log('just before form submit');
this.click('form[name="search"] input[type="submit"]'); //Again page refresh. Wait.
this.echo('Immediately after search btn getPresentState()' + this.evaluate(function() {
return getPresentState();
}));
this.echo('Immediately after search button-action' + this.evaluate(function() {
return document.search.action;
}));
this.capture("mnf.png");
});
casper.then(function() {
casper.page.injectJs('./jquery.js');
this.capture("resultspage.png");
this.echo('Page title is: ' + this.evaluate(function() {
return document.title;
}), 'INFO');
var a = casper.evaluate(function() {
return $('tbody tr td.tdbottom:contains("tye") ').siblings().filter($('td>a').parent());
});
console.log("ARBABU before" + a.length);
});
casper.run();
什麼是myElement的例子?如果你想等一個div,並且你在頁面上有很多div,該怎麼辦? – PositiveGuy
@WTF {myElement}可以是任何你想要的選擇器,例如,如果你想要一個特定的div,那麼你可以給它一個id'myDiv',然後等待#myDiv ... –