I’m working with selenium webdriver and Python on an Ubuntu server. Since I need to run everything in headless mode without a display, I’m using a virtual display setup.
My script keeps failing with a ‘Modal dialog present’ error whenever it tries to interact with page elements. Here’s the error I’m getting:
# My test script that's causing issues
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
try:
driver.get("https://example.com")
# This line fails with modal dialog error
element = WebDriverWait(driver, 10).until(
lambda d: d.find_element(By.CSS_SELECTOR, "#content-table")
)
except Exception as e:
print(f"Error occurred: {e}")
# WebDriverException: Message: 'Modal dialog present'
The error happens when the script tries to find elements on the page, but there seems to be some kind of popup or alert dialog blocking the interaction. How can I detect and dismiss these modal dialogs in headless selenium? What’s the proper way to handle popup windows that appear during automated testing?
I’ve dealt with this exact issue. Add an explicit wait for alerts using expected_conditions.alert_is_present() from selenium.webdriver.support. The modal might need a second to load while your script’s already moving on. Also, double-check if it’s actually a JS alert or just a CSS overlay - inspect the element first to see what you’re working with.
To address the issue of modal dialogs in headless mode, it’s essential to manage these alerts before interacting with other elements. Implement a try-catch block immediately after loading the page to check for alerts. Use driver.switch_to.alert
to reference the modal and then call .accept()
or .dismiss()
as necessary. Be aware that a NoAlertPresentException will be raised if no alert is present, so the try-catch is vital. It’s also beneficial to create a helper function that checks for alerts before any significant action, as some websites may trigger multiple popups while navigating. As a side note, using browser options like --disable-popup-blocking
can prevent certain dialogs from appearing, but it won’t resolve issues with JavaScript alerts.
I’ve hit this exact issue with automated tests in our CI/CD pipeline. Headless browsers still trigger JS alerts and confirm dialogs, but you can’t see them, which makes debugging a pain. Here’s what fixed it for me: handle alerts proactively right after page load. I wrapped my element interactions in a function that polls for alerts using driver.switch_to.alert
with a short timeout loop before grabbing elements. Also watch out - some sites use custom modal overlays instead of native browser alerts. You’ll need to check for CSS-based modals with selectors like div[role='dialog']
or common modal class names. If it’s a custom HTML modal instead of a JS alert, find and click the close button or overlay background to dismiss it before continuing your test.
This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.