     [Answers](https://scrapfly.io/blog)   /  [playwright](https://scrapfly.io/blog/tag/playwright)   /  [How to handle popup dialogs in Playwright?](https://scrapfly.io/blog/answers/how-to-click-on-alert-dialog-in-playwright)   # How to handle popup dialogs in Playwright?

 by [Bernardas Alisauskas](https://scrapfly.io/blog/author/bernardas) Apr 18, 2026 3 min read [\#playwright](https://scrapfly.io/blog/tag/playwright) [\#python](https://scrapfly.io/blog/tag/python) 

 [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-click-on-alert-dialog-in-playwright "Share on LinkedIn")    

 

 

In this guide, we'll explore handling popups in Playwright. To start, let's replicate a dialog example:

- Go to any product page one [web-scraping.dev](https://web-scraping.dev/product/1)
- Add the product to the cart
- Clear the cart

After following the above steps, you will encounter the below popup window:



First, we'll start by emulating the above popup event within Playwright:

Python

NodeJS

python```python
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False)
    page = browser.new_page()

    # add something to cart
    page.goto("https://web-scraping.dev/product/1")
    page.click(".add-to-cart")

    # clear out the cart to trigger the popup event
    page.goto("https://web-scraping.dev/cart")
    page.wait_for_selector(".cart-full .cart-item")
    page.click("(//button[contains(text(),'Clear')])[2]")
```





javascript```javascript
const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch({ headless: false });
  const page = await browser.newPage();

  // add something to cart
  await page.goto('https://web-scraping.dev/product/1');
  await page.click('.add-to-cart');

  // clear out the cart to trigger the popup event
  await page.goto('https://web-scraping.dev/cart');
  await page.waitForSelector('.cart-full .cart-item');
  await page.click("(//button[contains(text(),'Clear')])[2]");


  await browser.close();
})();
```







Here, start a new Playwright instance, and create a new page. Then, we use the new tab to request the product web page, add it to the cart, and clear it to trigger the popup dialog.

Next, let's intercept the dialog action to handle it:

Python

NodeJS

python```python
from playwright.sync_api import sync_playwright

# create a dialog handler that will check message text and press yes/no
def handle_dialog(dialog):
    if "clear your cart" in dialog.message:
        print(f'clicking "Yes" to {dialog.message}')
        dialog.accept()  # press "Yes"
    else:
        dialog.dismiss()  # press "No"
    page.on("dialog", handle_dialog)


with sync_playwright() as p:
    browser = p.chromium.launch(headless=False)
    page = browser.new_page()

    # intercept popup dialog with the handle_dialog function
    page.on("dialog", handle_dialog) # new

    # add something to cart
    page.goto("https://web-scraping.dev/product/1")
    page.click(".add-to-cart")

    # clear out the cart to trigger the popup event
    page.goto("https://web-scraping.dev/cart")
    page.wait_for_selector(".cart-full .cart-item")
    page.click("(//button[contains(text(),'Clear')])[2]")

    # verify that cart is clear
    print(f'items in cart: {page.query_selector(".cart-item .cart-title")}')  # should be None
```





javascript```javascript
const { chromium } = require('playwright');

(async () => {
    const browser = await chromium.launch({ headless: false });
    const page = await browser.newPage();

    // Create a dialog handler that will check the message text and press Yes/No
    page.on('dialog', async dialog => {
        if (dialog.message().includes('clear your cart')) {
            console.log(`clicking "Yes" to ${dialog.message()}`);
            await dialog.accept(); // press "Yes"
        } else {
            await dialog.dismiss(); // press "No"
        }
    });

    // Add something to cart
    await page.goto('https://web-scraping.dev/product/1');
    await page.click('.add-to-cart');

    // Clear out the cart to trigger the popup event
    await page.goto('https://web-scraping.dev/cart');
    await page.waitForSelector('.cart-full .cart-item');
    await page.click("(//button[contains(text(),'Clear')])[2]");

    // Verify that cart is clear
    const cartItem = await page.$('.cart-item .cart-title');
    console.log(`items in cart: ${cartItem}`);  // should be null

    await browser.close();
})();
```







Here, we define a `handle_dialog` function to click the pop up. Then, we intercept the dialog and click the popup with the method: `page.on("dialog", handle_dialog)`.

We can handle popups in Playwright successfully. For further details on web scraping with Playwright, refer to our dedicated guide.

[Web Scraping with Playwright and PythonPlaywright is the new, big browser automation toolkit - can it be used for web scraping? In this introduction article, we'll take a look how can we use Playwright and Python to scrape dynamic websites.](https://scrapfly.io/blog/posts/web-scraping-with-playwright-and-python)



 

    



Scale Your Web Scraping

Anti-bot bypass, browser rendering, and rotating proxies, all in one API. Start with 1,000 free credits.

  No credit card required  1,000 free API credits  Anti-bot bypass included 

 [Start Free](https://scrapfly.io/register) [View Docs](https://scrapfly.io/docs/onboarding) 

 Not ready? Get our newsletter instead. 

 

## Explore this Article with AI

 [ ChatGPT ](https://chat.openai.com/?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-click-on-alert-dialog-in-playwright) [ Gemini ](https://www.google.com/search?udm=50&aep=11&q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-click-on-alert-dialog-in-playwright) [ Grok ](https://x.com/i/grok?text=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-click-on-alert-dialog-in-playwright) [ Perplexity ](https://www.perplexity.ai/search/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-click-on-alert-dialog-in-playwright) [ Claude ](https://claude.ai/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-click-on-alert-dialog-in-playwright) 



 ## Related Articles

 [  

 http nodejs 

### Web Scraping With NodeJS and Javascript

In this article we'll take a look at scraping using Javascript through NodeJS. We'll cover common web scraping libraries...

 

 ](https://scrapfly.io/blog/posts/web-scraping-with-nodejs) [     

 python beautifulsoup 

### How to Scrape Ticketmaster

Learn how to scrape Ticketmaster for event data including concerts, venues, dates, and ticket information using Python. ...

 

 ](https://scrapfly.io/blog/posts/how-to-scrape-ticketmaster) [  

 tools hidden-api 

### Using API Clients For Web Scraping: Postman

In this article, we'll explore the use of API clients for web scraping. We'll start by explaining how to locate hidden A...

 

 ](https://scrapfly.io/blog/posts/using-api-clients-for-web-scraping-postman) 

  ## Related Questions

- [ Q How to handle popup dialogs in Puppeteer? ](https://scrapfly.io/blog/answers/how-to-click-on-alert-dialog-in-puppeteer)
- [ Q How to handle popup dialogs in Selenium? ](https://scrapfly.io/blog/answers/how-to-click-on-alert-dialog-in-selenium)
- [ Q How to scroll to the bottom of the page with Playwright? ](https://scrapfly.io/blog/answers/how-to-scroll-to-the-bottom-with-playwright)
- [ Q How to get file type of an URL in Python? ](https://scrapfly.io/blog/answers/how-to-get-url-filetype-in-python)
 
  



   



 Run headless browsers at scale, **1,000 free credits** [Start Free](https://scrapfly.io/register)