查找页面上给定文本的选择器

问题描述 投票:-1回答:1

我正在尝试以编程方式在网页上找到给定文本的选择器。

我已经摆弄XPATH和JQuery的.contains(),但找不到满意的答案。

例如,在网页www.stackoverflow.com上,对于文本“ Top Questions,以编程方式找到的选择器应为#mainbar > div:nth-child(1) > h1

我可以使用任何库,只要在使用Puppeteer进行爬网时就可以将它们注入页面中(只要它在CDN上可用就不会有问题)。

谢谢您的帮助🙏

javascript jquery html dom puppeteer
1个回答
0
投票

您可以使用containstext获得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');
© www.soinside.com 2019 - 2024. All rights reserved.