我正在尝试在网站上键入用户名,但输入框位于iframe内,我曾尝试使用此代码在iframe中定位该元素,但始终收到错误JSHandles can be evaluated only in the context they were created!
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto("www.examplesite.com", { waitUntil: 'networkidle0' })
await sleep(1000)
const myframe = await page.frames()[2];
const userselector = await myframe.$('input[name="usernameinput"]')
await page.type(userselector, "myusername")
await page.screenshot({path: 'example.png'});
await browser.close();
})();
答案 0 :(得分:1)
type
函数需要选择器字符串而不是句柄。因此,由于框架还具有type
函数,您可以执行以下操作:
await myframe.type('input[name="usernameinput"]', "myusername")