简体   繁体   English

Puppeteer - 根据文本选择孩子

[英]Puppeteer - select based on text children

If I have the following:如果我有以下几点:

<div>Hello world</div>

How can I select the div using "Hello world" as the query value?如何使用“Hello world”作为查询值选择 div?

You can try with jQuery's :contains() selector that selects all elements that contain the specified text :您可以尝试使用 jQuery 的:contains()选择器来选择包含指定文本的所有元素

 console.log($('div:contains(Hello world)').get(0));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div>Hello world</div>

alternatively, you can use XPath:或者,您可以使用 XPath:

$x('//div[text()="Hello world"]');

or, if you're not necessarily looking for the exact match:或者,如果您不一定要寻找完全匹配的:

$x('//div[text()[contains(., "Hello world")]]');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM