如何用木偶师在iframe中填写表格



好吧,我已经研究并发现了堆栈溢出中的一个类似问题,但我不理解它,因此我做错了,它不起作用,我有以下脚本:

const puppeteer = require('puppeteer');  
const fs = require('fs').promises;                                                                                                              
(async () => {
console.log("Iniciado!")                        
const browser = await 
puppeteer.launch({        
executablePath: '/usr/bin/chromium',            
headlesss:false,                                
});                                             
const page = await 
browser.newPage();           
await page.setViewport({ width: 1920, height: 1080 });                                          await  
page.setRequestInterceptiontrue);
page.on('request', (req) => {                   
if(req.resourceType() === 'image' || req.resourceType() === 'stylesheet' || req.resourceType() === 'font'){
req.abort();                                   
}                                               
else {                                           
req.continue();                               
}                                              
});
await page.goto('https://www.nike.com.br/chuteira-nike-premier-2-sala-unissex-153-169-171-309321', { waitUntil: 'load',timeout:0});             
await page.waitForXPath('//label[@for="tamanho__idM40F395"]',{visibility:true, timeout:0});    
const tamanho = await page.$x('//label[@for="tamanho__idM40F395"]')                             
await tamanho[0].click('//label[@for="tamanho__idM40F395"]');
await page.waitForSelector('button#anchor-acessar-unite-oauth2')
await page.click('button#anchor-acessar-unite-oauth2')
})();

运行这个脚本,它会选择运动鞋的尺寸并点击登录按钮,但如果它在iframe内,我该如何填写表格?我需要能够完成表格。

注意:

我删除了图片和css加载,以便页面加载更快。

您只需要找到iframe元素,获取其框架并使用它来代替页面:

const iframe = await page.waitForSelector('iframe#nike-unite-oauth2-iframe')
const frame = await iframe.contentFrame()
await frame.type('input[name="emailAddress"]', 'foo@bar.com')
await frame.type('input[name="password"]', '123')
await frame.click('input[value="ENTRAR"]')

你试过木偶师类型的api吗?从我所看到的,您的电子邮件和pw字段是带有id的输入字段。试试这个await page.type('#88650c48-8f4f-48b7-9ce3-48087cacfd1c', 'my_email'),密码字段也是如此。