首先,像你拥有的那样调用Nightmare - 分成两条链 - 是probably不会做你想做的事。 (这个评论区是一个很好的入门读物,虽然很长。)内存服务,来自第二个链的操作将在第一个链之后立即排队,从而导致(可能)不良行为。你说你的写法略有不同 - 我很想看看它,听起来可能更接近一些。
其次,你试图举起resultArr
in .evaluate()
,这是不可能的。该函数传递给.evaluate()
在 Electron 内部进行字符串化和重构 - 这意味着您将失去函数周围的环境上下文。这个例子 in nightmare-examples如果您好奇的话,可以更深入地了解一下。
第三,也许这是一个错字或我误解意图:你的href
选择器使用开头为 (^=
) 操作员,这是故意的吗?这是否应该是一个结尾($=
)?
Fourth, 循环异步操作很棘手。我的印象是这也可能是一个绊脚石?
考虑到所有这些,让我们看看修改原始脚本。诚然未经测试,因为我无权访问你的测试 URL,所以这有点随意:
var Nightmare = require('nightmare');
var nightmare = Nightmare({ openDevTools: true, show: true })
var Xray = require('x-ray');
var x = Xray();
nightmare
.goto(hidTestURL)
.wait(2500)
.click('input[name="propertySearchOptions:advanced"]') //start navigating to listing page
.wait(2500)
.type('input[name="propertySearchOptions:streetName"]', 'Main')
.wait(2500)
.select('select[name="propertySearchOptions:recordsPerPage"]', '25')
.wait(2500)
.click('input[name="propertySearchOptions:search"]') //at listing page
.wait(2500)
.evaluate(function(){
//using `Array.from` as the DOMList is not an array, but an array-like, sort of like `arguments`
//planning on using `Array.map()` in a moment
return Array.from(
//give me all of the elements where the href contains 'Property.aspx'
document.querySelectorAll('a[href*="Property.aspx"]'))
//pull the target hrefs for those anchors
.map(a => a.href);
})
.then(function(hrefs){
//here, there are two options:
// 1. you could navigate to each link, get the information you need, then navigate back, or
// 2. you could navigate straight to each link and get the information you need.
//I'm going to go with #1 as that's how it was in your original script.
//here, we're going to use the vanilla JS way of executing a series of promises in a sequence.
//for every href in hrefs,
return hrefs.reduce(function(accumulator, href){
//return the accumulated promise results, followed by...
return accumulator.then(function(results){
return nightmare
//click on the href
.click('a[href="'+href+'"]')
//get the html
.evaluate(function(){
return document.querySelector('html').innerHTML;
})
//add the result to the results
.then(function(html){
results.push(html);
return results;
})
.then(function(results){
//click on the search result link to go back to the search result page
return nightmare
.click('a[id="propertyHeading_searchResults"]')
.then(function() {
//make sure the results are returned
return results;
});
})
});
}, Promise.resolve([])) //kick off the reduce with a promise that resolves an empty array
})
.then(function (resultArr) {
//if I haven't made a mistake above with the `Array.reduce`, `resultArr` should now contain all of your links' results
console.log('resultArr', resultArr);
x(resultArr[1], 'body@html') //output listing page html
.write('results.json');
});
希望这足以让您开始。