我想转到登录页面,单击用户名,然后输入用户名。
这是我目前所拥有的:
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 中。所以首先您需要获取框架,然后从该框架中选择元素。你通常会这样做:
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 });
关于javascript - Puppeteer:找不到选择器的节点 - iframe 中的登录模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60192236/