     [Answers](https://scrapfly.io/blog)   /  [selenium](https://scrapfly.io/blog/tag/selenium)   /  [How to handle popup dialogs in Selenium?](https://scrapfly.io/blog/answers/how-to-click-on-alert-dialog-in-selenium)   # How to handle popup dialogs 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-alert-dialog-in-selenium "Share on LinkedIn")    

 

 

To handle browser dialog pop-ups in Selenium like this one seen on web-scraping.dev cart page:



We can explicitly wait for `alert_is_present()` expected condition. For example, let's take a look at a real alert pop up on the cart page of web-scraping.dev:

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 NoAlertPresentException

driver = webdriver.Chrome()

# navigate to the product page and add a product to the cart
driver.get("https://web-scraping.dev/product/1")
add_to_cart_button = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, ".add-to-cart"))
)
add_to_cart_button.click()

# navigate to the cart and attempt to clear it
driver.get("https://web-scraping.dev/cart")
cart_item = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, ".cart-full .cart-item"))
)
clear_cart_button = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, ".cart-full .cart-clear"))
)
clear_cart_button.click()

try:
    alert = WebDriverWait(driver, 10).until(EC.alert_is_present())
    if "clear your cart" in alert.text:
        print(f'clicking "Yes" to {alert.text}')
        alert.accept()  # press "Yes"
    else:
        alert.dismiss()  # press "No"
except NoAlertPresentException:
    print("No alert is present")

# print out the remaining items in the cart
cart_items = driver.find_elements(By.CSS_SELECTOR, ".cart-item .cart-title")
print(f"items in cart: {len(cart_items)}")  # should be 0 if the cart was cleared
driver.quit()
```



In the example above, we navigate to the product page and add a product to the cart. Then we navigate to the cart and attempt to clear it. If the alert is present, we check whether the alert message contains the text "clear your cart" and if so, we click "Yes" to clear the cart. Otherwise, we click "No" to cancel the dialog.



 

    



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-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-alert-dialog-in-selenium) [ 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-selenium) [ 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-selenium) [ Claude ](https://claude.ai/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-click-on-alert-dialog-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) [     

### How to Use cURL to Download Files

Curlhttps://curl.se/, short for "Client URL," is a versatile command-line tool used for transferring data with URLs. It'...

 

 ](https://scrapfly.io/blog/posts/how-to-curl-download-file) 

  ## Related Questions

- [ Q How to handle popup dialogs in Playwright? ](https://scrapfly.io/blog/answers/how-to-click-on-alert-dialog-in-playwright)
- [ 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 handle popup dialogs in Puppeteer? ](https://scrapfly.io/blog/answers/how-to-click-on-alert-dialog-in-puppeteer)
- [ 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)
 
  



   



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