简体   繁体   English

单击按钮后,CasperJS解析下一页

[英]CasperJS parse next page after button click

I use casperjs for grab some test. 我使用casperjs进行测试。

Algorithm is open URL parse page, click button for load next page. 算法是打开URL解析页面,单击按钮加载下一页。 How to grab next pages until test is complete. 在测试完成之前,如何抓取下一页。 All question get random and I don't now next question before form submitted. 所有问题都是随机的,在提交表单之前,我现在不再讨论下一个问题。

I need parse pages like a cycle or recursive. 我需要解析页面,例如循环或递归。 My code is: 我的代码是:

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

This code complete parse only one page, but doesn't redirect to next page and parse it. 这段代码仅完成对一页的解析,但不会重定向到下一页并对其进行解析。 What I do wrong? 我做错了什么?

EDIT: You need to nest the steps for the following pages, because on every page you evaluate if it is necessary to go further. 编辑:您需要嵌套以下页面的步骤,因为您评估每个页面上是否有必要进一步。 Also you should check the URL after you submitted the form. 提交表单后,您还应该检查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);

Exchange this code for your casper.then block. 将此代码交换为您的casper.then块。


Previous Answer: I don't know what kind of button/link #training is, but it may be that you need to wait for the change in the page to occur. 上一个答案:我不知道哪种按钮/链接#training是,但是可能您需要等待页面中的更改发生。 You could use the casper.waitForSelector function. 您可以使用casper.waitForSelector函数。

Also I'm not sure why you write 我也不确定你为什么写

this.evaluate(function () {
    $('input[type="submit"]:first').click();
});

and not simply this.click('input[type="submit"]:first'); 而不仅仅是this.click('input[type="submit"]:first'); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM