How to handle popup dialogs in Selenium?

by scrapecrow Jul 10, 2023

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

cart clear alert as seen on web-scraping.dev/cart

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:

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.

Related Articles

Bypass Proxy Detection with Browser Fingerprint Impersonation

Stop proxy blocks with browser fingerprint impersonation using this guide for Playwright, Selenium, curl-impersonate & Scrapfly

PROXIES
SELENIUM
PLAYWRIGHT
PUPPETEER
BLOCKING
Bypass Proxy Detection with Browser Fingerprint Impersonation

Guide to SeleniumBase — A Better & Easier Selenium

SeleniumBase streamlines browser automation with simple syntax, cross-browser support, and robust features, perfect for testing and web scraping.

SELENIUM
HEADLESS-BROWSER
Guide to SeleniumBase — A Better & Easier Selenium

Playwright vs Selenium

Explore the key differences between Playwright vs Selenium in terms of performance, web scraping, and automation testing for modern web applications.

HEADLESS-BROWSER
PLAYWRIGHT
SELENIUM
Playwright vs Selenium

What is a Headless Browser? Top 5 Headless Browser Tools

Quick overview of new emerging tech of browser automation - what exactly are these tools and how are they used in web scraping?

HEADLESS-BROWSER
PLAYWRIGHT
SELENIUM
PUPPETEER
What is a Headless Browser? Top 5 Headless Browser Tools

How to Scrape With Headless Firefox

Discover how to use headless Firefox with Selenium, Playwright, and Puppeteer for web scraping, including practical examples for each library.

HEADLESS-BROWSER
PUPPETEER
SELENIUM
NODEJS
PLAYWRIGHT
PYTHON
How to Scrape With Headless Firefox

Selenium Wire Tutorial: Intercept Background Requests

In this guide, we'll explore web scraping with Selenium Wire. We'll define what it is, how to install it, and how to use it to inspect and manipulate background requests.

PYTHON
HEADLESS-BROWSER
SELENIUM
TOOLS
Selenium Wire Tutorial: Intercept Background Requests