[英]Puppeteer: how to find element by text
I use the Puppeteer framework to automize tests.我使用 Puppeteer 框架来自动化测试。 I need to find an element as per a text:
我需要根据文本找到一个元素:
await page.focus(//span[contains(text(), 'Name')]])
<span class="some_placeholder">Name</span>
<span class="some_placeholder">Phone Number</span>
But in this case the following error occurs: "Evaluation failed: DOMException: Failed to execute 'querySelector' on Document '' is not a valid selector.." span[class='name'] finds some elements successfully but only one element is needed.但在这种情况下,会出现以下错误:“评估失败:DOMException:无法在文档上执行 'querySelector' 不是有效的选择器..” span[class='name'] 成功找到了一些元素,但只有一个元素是需要。
focus() accepts CSS selector and does not accept contains() ot text() methods. focus() 接受 CSS 选择器,不接受 contains() 或 text() 方法。
How should I code the search condition correctly?我应该如何正确编码搜索条件?
You caan use XPath with page.$x()
and then call elementHandle.focus()
:您可以将 XPath 与
page.$x()
,然后调用elementHandle.focus()
:
const [element] = await page.$x('//span[contains(text(), "Name")]');
await element.focus();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.