我要从网站上抓取多个div,然后在div符合我的标准时对其进行过滤-我想单击其中的一个按钮。
有我的代码:
const matchelems = async (elements, text) => {
try {
var matchings = []
for (var i = 0; i < elements.length; i++) {
let valueHandle = await elements[i].getProperty('innerText');
let linkText = await valueHandle.jsonValue();
if (linkText.indexOf(text) > -1) {
matchings.push(elements[i])
}
}
return matchings
} catch (e) {
console.log(e)
}
}
const elements = await page.$$(`div.foo`)
const filtered = await matchelems(elements, 'Find that')
console.log(`Filtered elements: ${filtered.length}`)
// now click the button in first div
const nestedButton = await filtered[0].$(`button.submit`);
console.log(nestedButton)
console.log(typeof(nestedButton))
await nestedButton.click()
控制台输出:
Filtered elements: 335
null
object
TypeError: Cannot read property 'click' of null
at dotask (C:\Users\Lenovo\Desktop\app\runtask.js:62:16)
at processTicksAndRejections (internal/process/next_tick.js:81:5)