does anybody know how to get the innerHTML or text of an element. Or even better; how to click an element with a specific innerHTML. This is how it would work with normal javascript:
var found = false
$(selector).each(function() {
if (found) return;
else if ($(this).text().replace(/[^0-9]/g, '') === '5' {
$(this).trigger('click');
found = true
}
Thanks in advance for any help!
You can leverage the
page.$$(selector)
to get all your target elments and then usepage.evaluate()
to get the content(innerHTML
), then apply your criteria. It should look something like:This should work with puppeteer:)
I can never get the .innerHtml to work reliable. I always do the following:
Then you have your text in the 'content' variable.
This is how i get innerHTML:
With regard to this part of your question...
There are some particulars around innerHTML, innerText, and textContent that might give you grief. Which you can work-around using a sufficiently loose XPath query with Puppeteer v1.1.1.
Something like this:
Just keep in mind that you will get an array of ElementHandles back from that query. So... the particular item you are looking for might not be at [0] if your text isn't unique.
Options passed to .click() aren't necessary if all you need is a single left-click.