gpt4 book ai didi

javascript - 使用casperjs时如何等待页面加载?

转载 作者:行者123 更新时间:2023-12-03 02:28:49 25 4
gpt4 key购买 nike

我正在尝试抓取一个网页,该网页的表单包含许多下拉菜单,并且表单中的值是相互依赖的。很多时候我需要代码等待页面刷新完成。例如,从列表中选择一个选项后,代码应该等待,直到根据该选择填充下一个列表。如果有人能够给出指示,那将非常有帮助,因为奇怪的是,我的代码只有在我给出了这么多不必要的日志记录语句之后才起作用,这反过来又造成了一些延迟。任何改进代码的建议都会非常有帮助。

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();

最佳答案

我一直在使用 Arun 在这里提到的 waitForSelector“解决方法”: https://stackoverflow.com/a/22217657/1842033

这是我找到的最好的解决方案; “缺点”是您需要知道您期望加载什么元素。我说的是缺点,就我个人而言,我不认为我遇到过这样的情况:我没有得到某种反馈说我正在等待的事情已经发生了

this.waitForSelector("{myElement}",
function pass () {
test.pass("Found {myElement}");
},
function fail () {
test.fail("Did not load element {myElement}");
},
20000 // timeout limit in milliseconds
);

虽然我猜你可以使用 waitForResource()如果您没有视觉反馈,或者类似的东西。

关于javascript - 使用casperjs时如何等待页面加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22163583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com