javascript Casperjs点击方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15757609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:00:31  来源:igfitidea点击:

Casperjs click method

javascriptweb-scrapingcasperjs

提问by Slater Victoroff

I'm pretty new to casperjs and javascript in general, but I have pretty extensive coding experience in other realms. Currently the code I'm trying to get running is just going to a website and clicking on a link, which should be straightforward, but I'm having trouble.

一般来说,我对 casperjs 和 javascript 还很陌生,但我在其他领域有相当丰富的编码经验。目前,我试图运行的代码只是访问网站并单击链接,这应该很简单,但我遇到了麻烦。

var casper = require('casper').create();
var x = require('casper').selectXPath;

casper.start('http://www.guru.com/emp/search.aspx?keyword=#&&page=1&sort=Earnings');

casper.then(function() {
    this.test.assertExists({
        type: 'xpath',
        path: '//*[@class="paddingLeft5 txt11px txt666"]/a[text()="Next"]'
    }, "Got Here");
});

casper.then(function() {
    var firstUrl = this.getCurrentUrl()
});

casper.thenClick(x('//*[@class="paddingLeft5 txt11px txt666"]/a[text()="Next"]'), function() {
    console.log("Woop!");
});

casper.waitFor(function check() {
    return this.evaluate(function() {
        return this.getCurrentUrl() != firstUrl;
    });
}, function then() {
    console.log(this.getCurrentUrl());
});


casper.run();

currently this times out after 5000ms without wrapping in the waitFor it simply prints the same url twice.

目前这会在 5000 毫秒后超时而没有包装在 waitFor 中,它只会打印相同的 url 两次。

回答by hexid

This should be what you are looking for. Note that I moved firstUrlto be a global variable; this way, the Casper.waitFor()has access to it.

这应该是你正在寻找的。请注意,我firstUrl变成了一个全局变量;这样,Casper.waitFor()就可以访问它。

Also, using this.evaluate()inside of the Casper.waitFor()was unnecessary and actually inhibiting receiving the error message because neither thisnor firstUrlexisted on the remote page. This is because any variables that you want to have access to inside of an Casper.evaluate()must be passed as arguments after the function.

此外,使用this.evaluate()insideCasper.waitFor()是不必要的,实际上禁止接收错误消息,因为远程页面上既不存在this也不firstUrl存在。这是因为您想要访问的任何变量都Casper.evaluate()必须在函数之后作为参数传递。

var casper = require('casper').create();
var x = require('casper').selectXPath;
var firstUrl;
casper.start('http://www.guru.com/emp/search.aspx?keyword=#&&page=1&sort=Earnings');

casper.then(function() {
    this.test.assertExists({
        type: 'xpath',
        path: '//*[@class="paddingLeft5 txt11px txt666"]/a[text()="Next"]'
    }, "Got Here");
});

casper.then(function() {
    firstUrl = this.getCurrentUrl()
});

casper.thenClick(x('//*[@class="paddingLeft5 txt11px txt666"]/a[text()="Next"]'), function() {
    console.log("Woop!");
});

casper.waitFor(function check() {
    return this.getCurrentUrl() != firstUrl;
}, function then() {
    console.log(this.getCurrentUrl());
});


casper.run();

This is the result that I get when running the code above:

这是我在运行上面的代码时得到的结果:

Woop!
http://www.guru.com/emp/search.aspx?keyword=#&&sort=Earnings&page=2

回答by NiKo

Looks like a website heavily relying on JavaScript for its navigation…

看起来像一个严重依赖 JavaScript 进行导航的网站……

You should probably try to waitForthe url to change before processing your next step.

在处理下一步之前,您可能应该尝试等待url 更改。