How to Set innerText By ElementHandle with Puppeteer?

Viewed 426

I'm trying to set the innerText of an HTML element in a Puppeteer test environment and cannot (easily) get the element by CSS selector, so I'm getting the elementHandle via:

let [ el ] = await page.$x(`//span[contains(text(), 'addTestFile.pdf')]`)

I normally set innerText for testing purposes using evaluate, but I need a selector for that, e.g.:

await page.evaluate(() => document.querySelector('mySelector').innerText = '⚠️DOM modified for testing purposes⚠️')

Can I set innerText of a given elementHandle without a CSS selector?

I see I can get the value with await el.getProperty('innerHTML') but I'm not finding a way to set it.

1 Answers

I figured it out: I can use evaluate on the elementHandle as so:

let [ el ] = await page.$x(`//span[contains(text(), 'addTestFile.pdf')]`)
await el.evaluate(element => element.innerText = '⚠️DOM modified for testing purposes⚠️')
Related