从页面内的ElementHandle获取输入类型。$$('input')

时间:2020-05-20 18:36:23

标签: javascript automation puppeteer

我已经设置了puppeteer来遍历所有标签,并在其中填充随机内容(或者在下面的代码中只是blabla)。

const puppeteer = require('puppeteer');
(async ()=> {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.roboform.com/filling-test-all-fields')
//^ Not my page, for test purposes only ^
const inputs = await page.$$('input');
for (let i = 0; i < inputs.length ; i++) {
  const curren = inputs[i];
  await curren.type('blablabla');
  //Find out curren's input type here
}
await page.screenshot({path: 'test.png'});
await browser.close();
})
();

现在,我需要知道输入标签的类型是什么:文本,密码,重置等。

console.log(await curren.getProperty('type'))下,它显示了_client: CDPSession { _remoteObject: { type: 'string', value: 'reset' } }的很多内容以及我实际需要的东西。我实际上如何单独在remoteObject中获得该值?

1 个答案:

答案 0 :(得分:1)

getProperty将返回JSHandle。如果您需要该JSHandle的值,则可以调用jsonValue()

const type = await (await curren.getProperty('type')).jsonValue();

如果您想在一次通话中做到这一点,则可以使用评估:

const type = await page.evaluate(el => el.getAttribute('type'), current);
相关问题