admin管理员组

文章数量:1287928

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!

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!

Share Improve this question edited Feb 26, 2020 at 16:07 hardkoded 21.6k3 gold badges60 silver badges74 bronze badges asked Sep 4, 2019 at 21:53 remed.ioremed.io 3041 gold badge2 silver badges9 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 16

Using evaluate should work:

console.log(await page.evaluate(x => x.value, fieldHandle)));

This worked in my case

const name = await page.$eval("#usernameInput", (input) => {
 return input.getAttribute("value")
 });

you can use this simply..

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

本文标签: javascriptHow to access current value in a text input field with puppeteerStack Overflow