Skip to content
Advertisement

How to access current value in a text input field with puppeteer

I’m trying to automate retrieving form values from an already filled out form with puppeteer and xpath.

I’ve already automated FILLING a text input field as follows, but doing the reverse with .evaluate() doesn’t work:

[fieldHandle] = await page.$x("//label[text() = 'My Label']/../following-sibling::td[1]//input")
await page.evaluate((x, y) => x.value = y, fieldHandle, 'newValue')

This is my most recent attempt – still no success…

let [fieldHandle] = await page.$x("//label[text() = 'My Label']/../following-sibling::td[1]//input")
let fieldRaw = await fieldHandle.getProperty('textContent')
let fieldValue = await fieldRaw.jsonValue()

Hopefully someone out there knows how to achieve this!

Advertisement

Answer

Using evaluate should work:

console.log(await page.evaluate(x => x.value, fieldHandle)));
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement