[英]Puppeteer: No node found for selector - Login Modal in iframe
我要go到登录页面,点击用户名,输入用户名。
这是我到目前为止所拥有的:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({ headless: false, slowMo: 200 });
const page = await browser.newPage();
await page.goto('https://fantasy.espn.com/basketball/team');
await page.waitFor(2000);
await page.click('#did-ui-view > div > section > section > form > section > div:nth-child(1) > div > label > span.input-wrapper > input');
await page.waitFor(2000);
await page.type('#did-ui-view > div > section > section > form > section > div:nth-child(1) > div > label > span.input-wrapper > input', 'hello', { delay: 100 });
await browser.close()
})();
我继续收到此错误:
Error: No node found for selector: #did-ui-view > div > section > section > form > section > div:nth-child(1) > div > label > span.input-wrapper > input
我不明白; 当我这样做时,控制台中的document.querySelector
会返回输入; 为什么找不到那个节点?
该元素在 iframe 中。所以首先您需要获取框架,然后 select 来自该框架的元素。 你通常会这样做:
let iframeHandle = await page.$('#disneyid-iframe');
let frame = await iframeHandle.contentFrame();
let inputElement = await frame.waitForSelector('#did-ui-view > div > section > section > form > section > div:nth-child(1) > div > label > span.input-wrapper > input')
await inputElement.type('hello', { delay: 100 });
但是,进程外框架存在问题,因此您需要使用--disable-features=site-per-process
启动 chromium。 以下应该工作:
const browser = await puppeteer.launch({headless: false, args: ['--disable-features=site-per-process']});
const page = await browser.newPage();
await page.goto('https://fantasy.espn.com/basketball/team', {waitUntil: ['load', 'domcontentloaded','networkidle0']});
await page.waitFor(5000);
let iframeHandle = await page.$('#disneyid-iframe');
let frame = await iframeHandle.contentFrame();
let inputElement = await frame.waitForSelector('#did-ui-view > div > section > section > form > section > div:nth-child(1) > div > label > span.input-wrapper > input')
await inputElement.type('hello', { delay: 100 });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.