meds
meds

Reputation: 22926

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?

Upvotes: 0

Views: 355

Answers (2)

mbit
mbit

Reputation: 3013

alternatively, you can use XPath:

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

or, if you're not necessarily looking for the exact match:

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

Upvotes: 1

Mamun
Mamun

Reputation: 68933

You can try with jQuery's :contains() selector that selects all elements that contain the specified text:

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>

Upvotes: 2

Related Questions