     [Answers](https://scrapfly.io/blog)   /  [selenium](https://scrapfly.io/blog/tag/selenium)   /  [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)   # How to click on cookie popups and modal alerts in Selenium?

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

 [  ](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-selenium "Share on LinkedIn")    

 

 

Modal pop-ups are usually encountered when dealing with cookie consent popups or login request walls when scraping using Selenium. The modal pop-up is created using custom javascript that hides the content on page load and shows some sort of message like this one:



There are multiple ways to handle modal pop-ups though these are the two most common ways:

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 selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

driver = webdriver.Chrome()

driver.get("https://web-scraping.dev/login")

# Option #1 - use driver.find_element() to click on the button
try:
    cookie_ok_button = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.ID, "cookie-ok")))
    cookie_ok_button.click()
except TimeoutException:
    print("no cookie popup")


# Option #2 - delete the popup HTML
#   remove pop up
try:
    cookie_modal = WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.ID, "cookieModal")))
    driver.execute_script("arguments[0].remove();", cookie_modal)
except TimeoutException:
    pass
#   remove grey backgdrop which covers the screen
try:
    modal_backdrop = WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.CLASS_NAME, "modal-backdrop")))
    driver.execute_script("arguments[0].remove();", modal_backdrop)
except TimeoutException:
    pass

driver.quit()

```



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-selenium) [ 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-selenium) [ 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-selenium) [ 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-selenium) [ 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-selenium) 



 ## Related Articles

 [  

 python headless-browser 

### Web Scraping with Selenium and Python

Introduction to web scraping dynamic javascript powered websites and web apps using Selenium browser automation library ...

 

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

 python selenium 

### Intro to Web Scraping Using Selenium Grid

In this guide, you will learn about installing and configuring Selenium Grid with Docker and how to use it for web scrap...

 

 ](https://scrapfly.io/blog/posts/intro-to-web-scraping-using-selenium-grid) [  

 python data-parsing 

### How to Parse Web Data with Python and Beautifulsoup

Beautifulsoup is one the most popular libraries in web scraping. In this tutorial, we'll take a hand-on overview of how ...

 

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

  ## Related Questions

- [ Q 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)
- [ 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 download a file with Puppeteer? ](https://scrapfly.io/blog/answers/how-to-download-file-with-puppeteer)
 
  



   



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