使用最新的nightmarejs版本。当我运行此代码时,它会冻结。目标只是获取一个元素,看看它是否具有href。
var hasHref = async function(nightmare) {
var selector = '.pagination > ul > li.active + li > a'
try {
var hasHref = await nightmare.evaluate((selector) => {
return document.querySelector(selector).hasAttribute('href')
}, selector)
return hasHref;
} catch(e) {
console.error(e);
nightmare.end()
}
}
const nightmare = Nightmare({ show: true })
nightmare.goto(mysite)
hrefBool = hasHref(nightmare)
console.log(hrefBool)
nightmare.wait(5000).end()
答案 0 :(得分:0)
我必须假设您已经正确定义了nightmare
和mysite
。然后,唯一需要注意的是,如果选择器找不到匹配项,则您将尝试获取hasAttribute
为null。相反,您可以这样写:
const element = document.querySelector(selector);
return element ? element.hasAttribute('href') : false;
否则,该代码对我有用,因此,如果您仍然遇到问题,则可能需要提供一些有关设置的更多详细信息。