简体   繁体   English

如何从 Puppeteer 中的按钮打开新的 window 页面?

[英]How do I open a new window page from a button in Puppeteer?

I'm trying to open a new window page from a button in Puppeteer.我正在尝试通过 Puppeteer 中的按钮打开一个新的 window 页面。

An example given: I'm logging to a website and the moment I click the button for the login a new fresh window page will pop-up, redirecting to the site the button is meant to be going.给出的示例:我正在登录一个网站,当我单击登录按钮时,将弹出一个新的 window 页面,重定向到该按钮要访问的站点。 How can I do it?我该怎么做?

You can do that by simply pressing Shift button while doing page.click And to catch the newly opened window you can use waitForTarget.您可以通过在执行page.click时简单地按 Shift 按钮来执行此操作,并且要捕获新打开的 window,您可以使用 waitForTarget。

const puppeteer = require('puppeteer')

;(async () => {
    const browser = await puppeteer.launch({
        headless: false,
        defaultViewport: null,
    })
    const context = browser.defaultBrowserContext()
    const page = (await context.pages())[0]
    await page.goto('https://www.amazon.com/gp/product/B093GQSVPX/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1', {waitUntil: 'load'})
    await page.waitForSelector('a[title="Add to List"]', {visible: true})
    await page.keyboard.down('Shift')
    await page.click('a[title="Add to List"]')
    await page.keyboard.up('Shift')
    const popup = await browser.waitForTarget(
        (target) => target.url().includes('www.amazon.com/ap/signin')
    )

    const popupPage = await popup.page()
    await popupPage.waitForSelector('a.a-link-expander[role="button"]')
    await popupPage.click('a.a-link-expander[role="button"]')
    await popupPage.click('input#continue[type="submit"]')
    await browser.close()
})()

I have issue to click anything or use command to do some action on new opened window, everything works from command above, it return value of popupPage.url(), so it detect its opened, but it goes to line of action.. and no error and nothin happens on window, any suggestion?我在单击任何内容或使用命令对新打开的 window 执行某些操作时遇到问题,上面的命令一切正常,它返回 popupPage.url() 的值,因此它检测到它已打开,但它会转到操作线.. 并且没有错误,window 上什么也没有发生,有什么建议吗?

await page.goto('https://twitter.com');

  await sleep(3000);
  const frame = page.frames().find(f => f.url().startsWith('https://accounts.google.com/gsi/button'));
  const acceptBtn = await frame.$('#container > div > div.nsm7Bb-HzV7m-LgbsSe-bN97Pc-sM5MNb.oXtfBe-l4eHX > span.nsm7Bb-HzV7m-LgbsSe-BPrWId');
  await acceptBtn.click();

  await sleep(5000);
  const popup = await browser.waitForTarget((target) => target.url().includes('accounts.google.com/v3/signin'));
  //await popup.click('#yDmH0d > c-wiz > div > div.eKnrVb > div > div.j663ec > div > form > span > section > div > div > div:nth-child(3) > button');
  const popupPage = await popup.page()
  console.log(popupPage.url());
  await popupPage.type('input[type="email"]', "hehehe", { delay: 200 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过打开新窗口执行OAuth请求,而不是从当前页面重定向用户? - How can I do OAuth request by open new window, instead of redirect user from current page? 如何通过单击弹出窗口中的按钮将打开弹出窗口的窗口重定向到新页面? - How do I redirect the window that opened a popup window to a new page by clicking a button in the popup window? 如何使用 Puppeteer 在 SPA 中检索新页面内容? - How Do I Retrieve New Page Content in an SPA using Puppeteer? 使用Iframe打开页面时,我需要从Google查看器中隐藏“在新窗口中打开”按钮 - I needed to Hide the 'Open in New Window' button from the google viewer while i open my page using Iframe 如何在新窗口中打开SAME页面,并将JavaScript与它们交叉? - How do I open the SAME page on a NEW window, and cross JavaScript with them? 如何在新页面上打开数据? - How do I open data on a new page? 在 puppeteer 中,如何拦截 window.open 之后的重定向? - In puppeteer, how do you intercept a redirect after window.open? 单击一个按钮时如何打开新的 window? - How to open a new window when i click one button? 如何在已有CSS和JavaScript的情况下打开新窗口? - How do I open a new window with CSS and JavaScript already there? 如何打开新窗口/选项卡并动态填充内容? - How do I open a new window/tab and dynamically fill it with content?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM