I want to get the value of this
<input type="hidden" value="12345" name="InitiationTT">
But since it does not have an ID, I would like to retrieve the value by name. I first tried this:
const cii = await page.$("InitiationTT");
const init = await page.evaluate(el => el.getAttribute("value"), cii);
which did not work and threw TypeError: Cannot read properties of null (reading 'getAttribute')
, so then I tried this:
const cii = page.evaluate(() => {document.querySelector("input[name=InitiationTT]").value});
which gave me Promise { <pending> }
and so I finally tried this:
const cii = document.querySelector("input[name=InitiationTT]").value;
Which gave me ReferenceError: document is not defined
so I am assuming that is because I am using NodeJS(?)
These 3 methods did not work and I do not know if Puppeteer has a way of getting a value of an element by it's name. Any help would be appreciated.
CodePudding user response:
Make sure your selector is correct. Furthermore, the value
property should be available on the <input>
element.
const r = await page.$eval('input[name="InitiationTT"]', ({ value }) => value); // 12345
Reference: How to get element value in puppeteer
Note: A similar question has been asked before: