如何在 puppeteer 中填写表格

How to fill out form in puppeteer

我在打开此页面时无法登录 https://www.nike.com.br/chuteira-nike-phantom-gt-elite-3d-unissex-153-169-171-316414?gridPosition=A1

转到另一个页面,我无法填写表格,谁能帮我填写登录表格?

(async () => {
 try{                                              
  console.log("Started!")                        
  const browser = await puppeteer.launch({         
   executablePathh:'/usr/bin/chromium', headless:false,                                
  });                                             
  const page = await browser.newPage();           
  await page.setViewport({ width: 1920, height: 1080 });                                          
  await page.setRequestInterception(true);     
  const blockedResourceTypes = ["image", "bacon", "imageset", "font", "stylesheet", "texttrack", "csp_report"]
  const blockedURLs = [
   ""
  ]
  const allowedRequest = req => !blockedResourceTypes.includes(req.resourceType()) && !blockedURLs.includes(req.url())                
  page.on('request', (req) => {                  
   if(allowedRequest(req)) {                       
    req.continue();                                
   }                                                  
   else {                                           
    req.abort();                                   
   }                                             
 });
 await page.goto('https://www.nike.com.br/chuteira-nike-phantom-gt-elite-3d-unissex-153-169-171-316414?gridPosition=A1', { 
  waitUntil: 'domcontentloaded', timeout:0});                           
 await page.waitForXPath('//label[@for="tamanho__idM40F395"]', { 
  visible: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')
const iframe = wait.ForSelector('iframe#nike-unite-oauth2-iframe')                    
const frame = await iframe.contentFrame()       
await frame.type('input[name="emailAddress"]', 'test@gmail.com')
await frame.type('input[name="password"]', 'pass')                                         
await frame.click('input[value="ENTRAR"]')

他们意识到,如果您 运行 脚本,它将转到登录页面,然后我无法填写表格

要登录新页面而不是 iframe,请尝试替换为:

await page.click('button#anchor-acessar-unite-oauth2')
const iframe = wait.ForSelector('iframe#nike-unite-oauth2-iframe')                    
const frame = await iframe.contentFrame()       
await frame.type('input[name="emailAddress"]', 'test@gmail.com')
await frame.type('input[name="password"]', 'pass')                                         
await frame.click('input[value="ENTRAR"]')

有了这个:

await Promise.all([
  page.click('button#anchor-acessar-unite-oauth2'),
  page.waitForNavigation(),
])
await page.waitForSelector('input[name="emailAddress"]')
await page.type('input[name="emailAddress"]', 'test@gmail.com')
await page.type('input[name="password"]', 'pass')
await page.click('input[value="ENTRAR"]')