Javascript CasperJS-在页面A上的弹出链接上循环,并在删除所有弹出链接后返回到页面A?

Javascript CasperJS-在页面A上的弹出链接上循环,并在删除所有弹出链接后返回到页面A?,javascript,web-scraping,casperjs,Javascript,Web Scraping,Casperjs,我有一个页面有9个弹出链接,我需要从每个弹出窗口提取信息。在这之后,我需要返回到A页并继续工作 我在for循环中使用了waitForPopup和withPopup,我认为这是打开和关闭每个弹出窗口的正确方法。但奇怪的是,我从第一个弹出窗口得到的信息重复了9次。我花了整整一个晚上才把它做好,但还是被卡住了。请看下面我的代码。谢谢 更新 我发现一个链接状态正是问题所在。 在我的脚本中,有一个while循环,我发现我的脚本将在单击之前跳转到while循环的末尾 供您参考: 1> The cod

我有一个页面有9个弹出链接,我需要从每个弹出窗口提取信息。在这之后,我需要返回到A页并继续工作

我在for循环中使用了waitForPopup和withPopup,我认为这是打开和关闭每个弹出窗口的正确方法。但奇怪的是,我从第一个弹出窗口得到的信息重复了9次。我花了整整一个晚上才把它做好,但还是被卡住了。请看下面我的代码。谢谢

更新 我发现一个链接状态正是问题所在。

在我的脚本中,有一个while循环,我发现我的脚本将在单击之前跳转到while循环的末尾

供您参考:

1> The code is part of the var suite_1 = function() { ...my code... }

2> The regex /fulltext/ here is based on the popup links 
(only the Gid value varies), like url=http://www.***.com/fulltext_form.aspx&Gid=788...

3> I also have some debug info.

[debug] [phantom] Mouse event 'mousedown' on selector: xpath selector: (//a[@class="main-ljwenzi"])[9]
[debug] [phantom] Mouse event 'mouseup' on selector: xpath selector: (//a[@class="main-ljwenzi"])[9]
[debug] [phantom] Mouse event 'click' on selector: xpath selector: (//a[@class="main-ljwenzi"])[9]
[debug] [phantom] Navigation requested: url=http://www.***.com/fulltext_form.aspx&Gid=788, type=LinkClicked, willNavigate=true, isMainFrame=false
……还有

[info] [phantom] Step anonymous 119/122 http://www.***.com/fulltext_form.aspx&Gid=252923 (HTTP 200)

The program is expected to open the link with Gid=788, but after some _step, it still open the Gid=252923, which is the first popup.
代码

this.then(function() {

    var count = this.getElementsInfo('a.main').length;
    this.echo(count + ' fulltext links found:', 'INFO');

    for (var i = 1; i < count + 1; i++) {

    //According to the output,
the program will run the this.capture 9 times,
before run the following lines of code 9 times

        this.capture('before the click - ' + i + '.png');

        this.thenClick(x('(//a[@class="main"])[' + i + ']'));

        this.waitForPopup(/fulltext/, function() {
            this.echo('Popup opened', 'INFO');
            this.echo(this.getTitle(), 'INFO');
        }, null, 10000);

        this.withPopup(/fulltext/, function() {

            this.echo(this.getTitle(), 'INFO');
            this.waitForSelector('#tbl_content_main', function() {
                // do something
            });
        });

        this.then(function() {
            this.echo('Back to the main page' + this.getTitle(), 'INFO_BAR');
        });
    }
});
this.then(function(){
var count=this.getElementsInfo('a.main').length;
this.echo(计数+找到的全文链接:','INFO');
对于(变量i=1;i
我举了一个简单的例子来测试您的案例:

HTML(popup.HTML):

它按预期工作


因此,您的代码有两个问题:

  • 应该将for循环中的整个代码包装成一个then
  • 应该使用另一个计数器而不是
    i
    来创建选择器(您可以回显
    i
    的值,以查看它始终是
    count
    。在我的情况下,这是3)
  • 并且与
    无关,然后单击
    。在我的示例代码中,您可以将
    单击
    替换为
    然后单击
    ,所有内容都保持不变。。。如果您查看一下
    的实现,然后单击
    ,您将发现我的声明背后的原因

    试试这个:

    this.then(function() {
    
        var count = this.getElementsInfo('a.main').length;
        this.echo(count + ' fulltext links found:', 'INFO');
        var counter = 1 // +
        for (var i = 1; i < count + 1; i++) {
            this.then(function() { //+
                this.capture('before the click - ' + counter + '.png');//edit
    
                this.thenClick(x('(//a[@class="main"])[' + counter + ']'));//edit
    
                this.waitForPopup(/fulltext/, function() {
                    this.echo('Popup opened', 'INFO');
                    this.echo(this.getTitle(), 'INFO');
                }, null, 10000);
    
                this.withPopup(/fulltext/, function() {
    
                    this.echo(this.getTitle(), 'INFO');
                    this.waitForSelector('#tbl_content_main', function() {
                        // do something
                    });
                });
    
                this.then(function() {
                    this.echo('Back to the main page' + this.getTitle(), 'INFO_BAR');
                });
                counter ++ //+
            })//+
        }
    });
    
    this.then(function(){
    var count=this.getElementsInfo('a.main').length;
    this.echo(计数+找到的全文链接:','INFO');
    变量计数器=1//+
    对于(变量i=1;i


    如果你对为什么要使用另一个计数器感到好奇,你可以在另一个计数器上阅读我的答案

    您好,Sayakiss,我试图用then函数包装代码,但失败了。它在选择器:xpath选择器(//a[@class=“main ljwenzi”])[10]
    上的鼠标事件“mousedown”停止,因为只有9个链接。似乎for循环只运行了9次,然后运行了代码的click部分。我的代码大约有200行,我可以把代码通过电子邮件发送给你吗?@user5404480你能上传整个脚本吗?像gist这样的东西就可以了。您可以专注于弹出窗口,并在另一个网站上编写一个简单的脚本(但它可以重现您的问题)。2.如果您懒惰,可以将代码发送到我的电子邮件:
    zhushuierjirou@gmail.com
    @Sayakiss,如我在更新部分所述。弹出部分正常(通过屏幕截图和简单测试验证),问题可能是由单击引起的。@用户5404480我认为您应该使用另一个计数器,而不是
    I
    ,来生成
    x(“(//a[@class=“main”])['+I+']”
    。因为
    I
    在您接下来的步骤中总是
    计数
    。。。
    
    var casper = require('casper').create();
    
    casper.start('http://localhost:63342/popup.html')
    
    casper.then(function () {
        var count = casper.getElementsInfo('.popup').length
        var counter = 1
        for (var i = 0; i < count; i++) {
            casper.then(function () {
                var selector = 'body > div > button:nth-child(' + counter + ')';
                var text = casper.getElementInfo(selector).text
                casper.click(selector)
                casper.waitForPopup(text, undefined, undefined, 20000)
                casper.withPopup(text, function () {
                    casper.echo(casper.getTitle())
                })
                counter ++
            })
        }
    })
    
    casper.run()
    
    $ casperjs popup.js 
    CasperJS, a navigation scripting and testing utility for PhantomJS and SlimerJS
    PhantomJS | PhantomJS
    Node.js
    
    this.then(function() {
    
        var count = this.getElementsInfo('a.main').length;
        this.echo(count + ' fulltext links found:', 'INFO');
        var counter = 1 // +
        for (var i = 1; i < count + 1; i++) {
            this.then(function() { //+
                this.capture('before the click - ' + counter + '.png');//edit
    
                this.thenClick(x('(//a[@class="main"])[' + counter + ']'));//edit
    
                this.waitForPopup(/fulltext/, function() {
                    this.echo('Popup opened', 'INFO');
                    this.echo(this.getTitle(), 'INFO');
                }, null, 10000);
    
                this.withPopup(/fulltext/, function() {
    
                    this.echo(this.getTitle(), 'INFO');
                    this.waitForSelector('#tbl_content_main', function() {
                        // do something
                    });
                });
    
                this.then(function() {
                    this.echo('Back to the main page' + this.getTitle(), 'INFO_BAR');
                });
                counter ++ //+
            })//+
        }
    });