我试图弄清楚如何让脚本点击链接,转到某个页面,然后执行一些操作。下面是我被困住的例子,这是行不通的。
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });
nightmare
.goto("https://www.google.com/")
.type("input", "nightmare.js")
.wait(3000)
.click("button[type=submit]")
.wait(2000)
.evaluate(function(){
var title = document.querySelectorAll("h3 a");
i = 0;
if (title) {
title[i].addEventListener("click", function(){
setTimeout(function(){
alert("Success!");
}, 5000);
});
}
})
.then(function(result){
console.log("result", result);
})
.catch(function (error) {
console.error('Search failed:', error);
});正如你所看到的,它甚至不会转到下一页。
但是,如果像这样定义单击,它将转到下一页,但我也需要它在另一个页面上执行一些功能:
.evaluate(function(){
var title = document.querySelectorAll("h3 a");
i = 0;
if (title) {
title[i].click();
}
})所以这让我很困惑,不知道为什么不行。
发布于 2016-08-25 21:21:56
好的,我认为主要的问题是你如何点击那个链接。现在,在噩梦链回调的主流程之外执行此操作(.goto().type().wait().链子)如果您更改流以处理链,并再次使用.click()而不是.evaluate(),则可以在下一页上执行一个操作:
nightmare
.goto("https://www.google.com/")
.type("input", "nightmare.js")
.wait(3000)
.click("button[type=submit]")
.wait("h3[class=r]")
.click("h3[class=r] a")
.evaluate(function(){
return document.querySelector("h1 a").innerHTML
})
.end()
.then(function(result){
console.log("result", result);
})
.catch(function (error) {
console.error('Search failed:', error);
});这将输出与噩梦页面的<h1></h1>标记链接的内容,即“噩梦”
https://stackoverflow.com/questions/39154224
复制相似问题