[英]CasperJS parse next page after button click
我使用casperjs进行测试。
算法是打开URL解析页面,单击按钮加载下一页。 在测试完成之前,如何抓取下一页。 所有问题都是随机的,在提交表单之前,我现在不再讨论下一个问题。
我需要解析页面,例如循环或递归。 我的代码是:
casper.start(startUrl, function () {
this.click('#training');
this.evaluate(function () {
$('input[type="submit"]:first').click();
});
});
casper.then(function () {
var currentUrl = this.getCurrentUrl(),
startIdPos = currentUrl.indexOf('=') + 1,
questionId = currentUrl.slice(startIdPos),
content = $(this.getHTML()),
answers = [],
question,
startCorrectAnswerPos = content.find('script:nth-child(2)').html().indexOf('var bc='),
correctAnswer = content.find('script:nth-child(2)').html().slice(startCorrectAnswerPos + 8, startCorrectAnswerPos + 9);
question = content.find('table.quizz p.qw').html();
console.log(">>>>>>" + this.getCurrentUrl());
this.fill('form', {
'answer': correctAnswer
}, true);
});
casper.run();
这段代码仅完成对一页的解析,但不会重定向到下一页并对其进行解析。 我做错了什么?
编辑:您需要嵌套以下页面的步骤,因为您评估每个页面上是否有必要进一步。 提交表单后,您还应该检查URL。
function answer() {
var currentUrl = this.getCurrentUrl(),
startIdPos = currentUrl.indexOf('=') + 1,
questionId = currentUrl.slice(startIdPos),
content = $(this.getHTML()),
answers = [],
question,
startCorrectAnswerPos = content.find('script:nth-child(2)').html().indexOf('var bc='),
correctAnswer = content.find('script:nth-child(2)').html().slice(startCorrectAnswerPos + 8, startCorrectAnswerPos + 9);
question = content.find('table.quizz p.qw').html();
console.log(">>>>>>" + this.getCurrentUrl());
if (question) {
this.then(function(){
this.fill('form', {
'answer': correctAnswer
}, true);
});
this.then(answer);
}
};
casper.then(answer);
将此代码交换为您的casper.then
块。
上一个答案:我不知道哪种按钮/链接#training
是,但是可能您需要等待页面中的更改发生。 您可以使用casper.waitForSelector
函数。
我也不确定你为什么写
this.evaluate(function () {
$('input[type="submit"]:first').click();
});
而不仅仅是this.click('input[type="submit"]:first');
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.