gpt4 book ai didi

nightmare - 如何使用NightmareJS 3.0.1从Option标记中选择innerText和值

转载 作者:行者123 更新时间:2023-12-03 13:23:39 25 4
gpt4 key购买 nike

我正在研究某些东西,发现可以轻松地将两个返回的数组连接起来,但它不是很干……。您将如何处理呢???我需要选项文本和值,因为它们是不同的东西,但需要将它们返回到同一对象中,以便我可以处理返回的值……如果我能以某种方式将其映射简化,那么最好函数返回的键/值对象会更好,但是我不确定这是可能的:

//define a new Nightmare method named "textExtract"
//note that it takes a selector as a parameter
Nightmare.action('textExtract', function(selector, done) {
//`this` is the Nightmare instance
this.evaluate_now((selector) => {
//query the document for all elements that match `selector`
//note that `document.querySelectorAll` returns a DOM list, not an array
//as such, convert the result to an Array with `Array.from`
//return the array result
text = Array.from(document.querySelectorAll(selector))
//extract and return the text for each element matched
.map((element) => element.text );
elValues = Array.from(document.querySelectorAll(selector))
//extract and return the text for each element matched
.map((element) => element.value );
return text.concat(elValues)
//pass done as the first argument, other arguments following
}, done, selector)
});

referenced from github。

最佳答案

这个怎么样:

Nightmare.action('textExtract', function(selector, done) {
this.evaluate_now((selector) => {
return Array.from(document.querySelectorAll(selector))
.map(o => ({value: o.value, text: o.text}))
}, done, selector)
})

结果如下:
[
{ text: 'Bob', value: '766' },
{ text: 'Renee', value: '768' },
{ text: 'Paul', value: '787' }
]

关于nightmare - 如何使用NightmareJS 3.0.1从Option标记中选择innerText和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55699765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com