Puppeteer-错误:评估失败:domexception:无法执行QuerySelector'
我正在尝试使用 puppeteer $$eval
选择一些嵌套的 div。我已经复制了需要选择的 DOm 元素的 XPath,但似乎不起作用。
//XPath //*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a/span
await page.waitForSelector('//*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a/span')
const names = await page.$$eval('//*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a/span', (el) => {
return el.map( n => n.innerHTML )
})
citiesList.push(names)
//XPath //*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a
await page.waitForSelector('//*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a')
const href = await page.$$eval('//*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a', (el) => {
return el.map( a => a.getAttribute('href') )
})
citiesLinks.push(href)
使用此代码,我总是会收到此错误
Error: Evaluation failed: DOMException: Failed to execute 'querySelector' on 'Document': '//*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a/span' is not a valid selector
如何正确使用 xpath 和 $$eval
?
I'm trying to select some nested divs using puppeteer $$eval
. I've copied the XPath of the DOm elements I need to select but seems not working.
//XPath //*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a/span
await page.waitForSelector('//*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a/span')
const names = await page.$eval('//*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a/span', (el) => {
return el.map( n => n.innerHTML )
})
citiesList.push(names)
//XPath //*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a
await page.waitForSelector('//*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a')
const href = await page.$eval('//*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a', (el) => {
return el.map( a => a.getAttribute('href') )
})
citiesLinks.push(href)
With this code I will get always this error
Error: Evaluation failed: DOMException: Failed to execute 'querySelector' on 'Document': '//*[@id="app"]/main/div/div/div[2]/div/div/div/div[1]/ul/li/a/span' is not a valid selector
How I can use xpath with $$eval
correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过直接定位我需要的 DOM 元素来解决这个问题。
就我而言,我有一个带有
.list-item
引导类的li
标记。I've solved by targeting directly the DOM elements I need.
In my case I have an
li
tag with.list-item
bootstrap class.