单击按钮后,CasperJS将解析下一页 [英] CasperJS parse next page after button click

查看:73
本文介绍了单击按钮后,CasperJS将解析下一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用casperjs来获取一些测试。

I use casperjs for grab some test.

算法是打开的URL解析页面,点击按钮加载下一页。
如何在测试完成之前抓住下一页。
所有问题都是随机的,在提交表单之前我现在没有下一个问题。

Algorithm is open URL parse page, click button for load next page. 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?

推荐答案

编辑:您需要为以下页面,因为在每个页面上,您评估是否有必要更进一步。您还应该在提交表单后检查URL。

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.

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 函数。

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. You could use the casper.waitForSelector function.

我也不确定你为什么写

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

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

这篇关于单击按钮后,CasperJS将解析下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆