我想得到這個的價值
<input type="hidden" value="12345" name="InitiationTT">
但由于它沒有 ID,我想按名稱檢索值。我首先嘗試了這個:
const cii = await page.$("InitiationTT");
const init = await page.evaluate(el => el.getAttribute("value"), cii);
這不起作用并拋出了TypeError: Cannot read properties of null (reading 'getAttribute'),所以我嘗試了這個:
const cii = page.evaluate(() => {document.querySelector("input[name=InitiationTT]").value});
這給了我Promise { <pending> },所以我終于嘗試了這個:
const cii = document.querySelector("input[name=InitiationTT]").value;
這給了我ReferenceError: document is not defined所以我假設這是因為我使用的是 NodeJS(?)
這 3 種方法不起作用,我不知道 Puppeteer 是否可以通過名稱獲取元素的值。任何幫助,將不勝感激。
uj5u.com熱心網友回復:
確保您的選擇器是正確的。此外,該value屬性應該在<input>元素上可用。
const r = await page.$eval('input[name="InitiationTT"]', ({ value }) => value); // 12345
參考: 如何在 puppeteer 中獲取元素值
注意:之前有人問過類似的問題:
使用 Puppeteer 獲取 HTML 屬性的值
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/339488.html
標籤:javascript 节点.js 傀儡师
上一篇:即使使用process.on(uncaughtException),Nodejs行程也會崩潰
下一篇:如何計算node.js中的值
