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