12

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!

hardkoded
  • 18,915
  • 3
  • 52
  • 64
remed.io
  • 168
  • 1
  • 1
  • 9

3 Answers3

13

Using evaluate should work:

console.log(await page.evaluate(x => x.value, fieldHandle)));
hardkoded
  • 18,915
  • 3
  • 52
  • 64
  • 1
    Boom! That worked... Assigning the .evaluate() result to variable was successful! `let fieldValue = await page.evaluate(x => x.value, fieldHandle)` – remed.io Sep 05 '19 at 15:15
4

This worked in my case

const name = await page.$eval("#usernameInput", (input) => {
 return input.getAttribute("value")
 });
peanutz
  • 346
  • 3
  • 6
0

you can use this simply..

const name = await page.$eval('//label[text() = 'My Label']/../following-sibling::td[1]//input"]', element => element.value);