簡體   English   中英

Puppeteer:未找到選擇器的節點 - iframe 中的登錄模式

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM