CasperJS-在尝试通过循环填充下拉菜单时访问页面的内容 [英] CasperJS - Access page's content while trying to fill drop-down menu through a loop

查看:87
本文介绍了CasperJS-在尝试通过循环填充下拉菜单时访问页面的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用casperjs进行测试,这里的情况是:

I'm trying some tests with casperjs and the certain situation here is:



  • 从下拉菜单中提取城市名称,(已完成)

  • extracting city names from a drop-down menu, (Already Done)

然后选择每个城市(使用 casper.fill()),导致在页面上加载新的
内容和URL更改(成功,同时使用单个城市名称进行测试,失败

then select each city (with casper.fill()) which leads to load new contents and URL change on the page, (Successful while testing with a single city name, Failed with loop through the list of cities' names)

通过新加载项的链接(新页面)进一步上一层,

go one level further through new loaded items' links (new pages),

最后,从每个页面中抓取内容

finally, grab the content from each single page

我试图做一个循环以遍历城市清单,并在每个周期中完成所有工作。但是问题是 CasperJs 试图立即将每个城市的< option> 字段值设置为一个城市,而没有执行循环内的其余代码:

I was trying to do a loop to iterate through cities list and do all the work in each cycle. But the problem is CasperJs attempts to set <option> field value to each city one after another immediately and without executing the rest of the code inside the loop:

casper.then(function() {

    var citiesLength = cities.length;

    for (var i = 0; i < citiesLength; i++) {

        this.fill('form.wpv-filter-form',{   //setting drop-down field value to the city names in order of the items in the array
            'city[]': cityNames[i]
        });

// Apparently the code below (to the end of the loop) doesn't get executed
        casper.thenEvaluate(function() {

// Here the url change is being checked to know when the new content is loaded:
            var regexString = '(\\?)(city)(\\[\\])(=)(' + cityNames[i] + ')&';
            var regex = new RegExp(regexString, "igm");

            this.waitForUrl(regex, function(){
                var name = this.getHTML('.kw-details-title');
                link = this.evaluate(getFirstItemLink); // for test, just getting the first item's link

                casper.open(link).then(function(){
                    this.echo("New Page is loaded......");
                    // Grab the single item contents
                });
            });

        });
    }

这是日志(缩短了3个城市):

This is the log (Shortened for 3 cities):

[debug] [remote] Set "city[]" field value to city1
[info] [remote] attempting to fetch form element from selector: 'form.wpv-filter-form'
[debug] [remote] Set "city[]" field value to city2
[info] [remote] attempting to fetch form element from selector: 'form.wpv-filter-form'
[debug] [remote] Set "city[]" field value to city3
[info] [remote] attempting to fetch form element from selector: 'form.wpv-filter-form'
[info] [remote] attempting to fetch form element from selector: 'form.wpv-filter-form'
[info] [remote] attempting to fetch form element from selector: 'form.wpv-filter-form'
[info] [phantom] Step anonymous 5/5: done in 123069ms.
[info] [phantom] Step _step 6/79 https ://domain.com/section/ (HTTP 200)
[info] [phantom] Step _step 6/79: done in 123078ms.

Ps:是 casper.open()到达第二级页面(项目页面)的好方法吗?接受内容后,我是否需要以某种方式关闭它们?

P.s: Is the usage of casper.open() a good way to reach second level pages (item pages)? Do I need to close them somehow after taking their content?

谢谢

推荐答案

您的代码中有很多问题。就像不匹配步骤( then * wait * 函数)一样,这意味着您将直接调用( casper.fill )加上一个步骤( thenEvaluate )。

You have many issues in your code. Like not matching steps (then* and wait* functions) together which means that you mix direct invocation (casper.fill) with a step (thenEvaluate).

另一个问题是 this 不在页面上下文内引用 casper (在内部评估 thenEvaluate )。

The other issue is that this doesn't refer to casper inside of the page context (inside evaluate and thenEvaluate).

这应该有效:

cityNames.forEach(function(cityName){
    casper.then(function(){
        this.fill('form.wpv-filter-form', {   //setting drop-down field value to the city names in order of the items in the array
            'city[]': cityName
        });
    });

    casper.then(function(){
        var regexString = '(\\?)(city)(\\[\\])(=)(' + cityName + ')&';
        var regex = new RegExp(regexString, "igm");
        this.waitForUrl(regex, function(){
            var name = this.getHTML('.kw-details-title');
            link = this.evaluate(getFirstItemLink); // for test, just getting the first item's link

            this.thenOpen(link).then(function(){
                this.echo("New Page is loaded......");
                // Grab the single item contents
            });
        });
    });
});

这篇关于CasperJS-在尝试通过循环填充下拉菜单时访问页面的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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