Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle popups in puppeteer

Tags:

how to handle the popup and access the popup to do some operations on it.

const puppeteer = require('puppeteer');

async function run() {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://example.com');
    await page.click(Launchpopup);
}
like image 868
vinay kumar Avatar asked Oct 10 '17 14:10

vinay kumar


People also ask

How do you handle modal pop in puppeteers?

What you can do solve your issue is to create a new async function and inside of it you can put Puppeteer's page. evaluate() function. Then click on the button and load up the modal (grab the modal by its aria-labbelledby attribute) and put its contents inside a variable.


2 Answers

So what I do is I login to facebook on their homepage, then navigate to the page I want to go to where the sign in with facebook button is I click on it. And then this code below will once the popup happens click on the login with facebook button.

await page.click('[service_name="facebook"]')
const newPagePromise = new Promise(x => browser.once('targetcreated', target => x(target.page()))); 
const popup = await newPagePromise;
await popup.waitForSelector('[name="__CONFIRM__"]')
const confirm = await popup.$('[name="__CONFIRM__"]')
await popup.click('[name="__CONFIRM__"]')
await page.waitFor(2000);
await page.goto('your login page'); $
like image 133
luneth777 Avatar answered Sep 16 '22 16:09

luneth777


Since version 0.13.0, you can use the following code:

... code to open popup...
const pages = await browser.pages(); // get all open pages by the browser
const popup = pages[pages.length - 1]; // the popup should be the last page opened
like image 40
user3453261 Avatar answered Sep 20 '22 16:09

user3453261