我正在尝试以编程方式在网页上找到给定文本的选择器。
我已经摆弄XPATH和JQuery的.contains()
,但找不到满意的答案。
例如,在网页www.stackoverflow.com上,对于文本“ Top Questions,以编程方式找到的选择器应为#mainbar > div:nth-child(1) > h1
。
我可以使用任何库,只要在使用Puppeteer进行爬网时就可以将它们注入页面中(只要它在CDN上可用就不会有问题)。
谢谢您的帮助🙏
您可以使用contains
和text
获得DOM元素
await page.goto("https://stackoverflow.com/questions", { waitUntil: 'networkidle2' });
const element = await page.waitForXPath("//*[contains(text(),'All Questions')]");
await element.evaluate(e => e.textContent = 'Foo');