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!
How it works. First, get the <ul> element with the id menu using the getElementById() method. Second, create a new <li> element and add it to the <ul> element using the createElement() and appendChild() methods. Third, get the HTML of the <ul> element using the innerHTML property of the <ul> element.
This is how i get innerHTML:
page.$eval(selector, (element) => { return element.innerHTML })
You can use the following methods to return the innerHTML
of an element:
const inner_html = await page.$eval('#example', element => element.innerHTML);
const inner_html = await page.evaluate(() => document.querySelector('#example').innerHTML);
const element = await page.$('#example'); const element_property = await element.getProperty('innerHTML'); const inner_html = await element_property.jsonValue();
You can use the following methods to click on an element based on the innerHTML
that is contained within the element:
await page.$$eval('.example', elements => { const element = elements.find(element => element.innerHTML === '<h1>Hello, world!</h1>'); element.click(); });
await page.evaluate(() => { const elements = [...document.querySelectorAll('.example')]; const element = elements.find(element => element.innerHTML === '<h1>Hello, world!</h1>'); element.click(); });
const element = await page.evaluateHandle(() => { const elements = [...document.querySelectorAll('.example')]; const element = elements.find(element => element.innerHTML === '<h1>Hello, world!</h1>'); return element; }); await element.click();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With