     [Answers](https://scrapfly.io/blog)   /  [playwright](https://scrapfly.io/blog/tag/playwright)   /  [How to click on cookie popups and modal alerts in Playwright?](https://scrapfly.io/blog/answers/how-to-click-on-modal-alerts-like-cookie-pop-up-in-playwright)   # How to click on cookie popups and modal alerts in Playwright?

 by [Bernardas Alisauskas](https://scrapfly.io/blog/author/bernardas) Jul 10, 2023 1 min read [\#playwright](https://scrapfly.io/blog/tag/playwright) 

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

 

 

Modal pop-us are most commonly encountered as cookie consent popups or request to login popups. They are created using custom javascript that hide the content on page load and show some sort of message like this one:



There are multiple ways to handle modal pop-ups:

1. We can click on one of the values like "OK" or "Yes"
2. We can delete the modal element from the DOM

For example, let's take a look at [web-scraping.dev/login](https://web-scraping.dev/login) page which on page load throws a cookie pop-up:

python```python
from playwright.sync_api import sync_playwright, TimeoutError

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False)
    page = browser.new_page()
    page.goto("https://web-scraping.dev/login")

    # Option #1 - use page.click() to click on the button
    try:
        page.click("#cookie-ok", timeout=2_000)
    except TimeoutError:
        print("no cookie popup")

    # Option #2 - delete the popup HTML
    #   remove pop up
    cookie_modal = page.query_selector("#cookieModal")
    if cookie_modal:
        cookie_modal.evaluate("el => el.remove()")
    #   remove grey backgdrop which covers the screen
    modal_backdrop = page.query_selector(".modal-backdrop")
    if modal_backdrop:
        modal_backdrop.evaluate("el => el.remove()")
```



Above, we explore two ways to handle modal pop-ups: clicking a button that would dismiss it and hard removing them from the DOM. Generally, the first approach is more reliable as the real button click can have functionality attached to it like setting a cookie so the pop-up doesn't appear again. For cases when it's a login requirement or advertisement the second approach is more suited.



 

    



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-modal-alerts-like-cookie-pop-up-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-modal-alerts-like-cookie-pop-up-in-playwright) [ Grok ](https://x.com/i/grok?text=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-click-on-modal-alerts-like-cookie-pop-up-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-modal-alerts-like-cookie-pop-up-in-playwright) [ Claude ](https://claude.ai/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-click-on-modal-alerts-like-cookie-pop-up-in-playwright) 



 ## Related Articles

 [  

 nodejs headless-browser 

### Web Scraping with Playwright and JavaScript

Learn about Playwright - a browser automation toolkit for server side Javascript like NodeJS, Deno or Bun.

 

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

 python headless-browser 

### Web Scraping with Playwright and Python

Playwright is the new, big browser automation toolkit - can it be used for web scraping? In this introduction article, w...

 

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

 python nodejs 

### Playwright Examples for Web Scraping and Automation

Learn Playwright with Python and JavaScript examples for automating browsers like Chromium, WebKit, and Firefox.

 

 ](https://scrapfly.io/blog/posts/playwright-examples-javascript-and-python) 

  ## Related Questions

- [ Q How to click on cookie popups and modal alerts in Selenium? ](https://scrapfly.io/blog/answers/how-to-click-on-modal-alerts-like-cookie-pop-up-in-selenium)
- [ Q How to click on cookie popups and modal alerts in Puppeteer? ](https://scrapfly.io/blog/answers/how-to-click-on-modal-alerts-like-cookie-pop-up-in-puppeteer)
- [ Q How to download a file with Playwright and Python? ](https://scrapfly.io/blog/answers/how-to-download-file-with-playwright)
- [ Q How to handle popup dialogs in Selenium? ](https://scrapfly.io/blog/answers/how-to-click-on-alert-dialog-in-selenium)
 
  



   



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