Accept the native JavaScript alert first, then call Selenium’s screenshot method on the WebDriver. In Python, wait for the alert with alert_is_present(), call accept(), and save with driver.save_screenshot("page.png"). An alert is a browser dialog, not a page element you can locate with a CSS selector.
Python: accept the alert, then save the screenshot
This example assumes you have already created driver, navigated to the page, and performed the action that triggers the alert. It waits up to 10 seconds for the dialog before accepting it.
As an Amazon Associate I earn from qualifying purchases.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Assume driver has navigated to the page and an action causes the alert.
alert = WebDriverWait(driver, 10).until(EC.alert_is_present())
alert.accept()
driver.save_screenshot("page.png")
Use the wait when the dialog appears asynchronously or intermittently. If the triggering action is known to produce the alert immediately, you can switch to it directly:
driver.switch_to.alert.accept()
driver.save_screenshot("page.png")
The Python alert API also documents the equivalent Alert(driver).accept() form. Selenium captures the current browsing context; do not assume the result is a full-page image across every browser and driver.
#1 Best Overall
Java: accept the alert and capture the driver
With Java, get the alert from switchTo().alert(), accept it, then ask the driver for a screenshot. Selenium’s documented file-output approach returns a temporary file, which you copy to your chosen destination.
import java.io.File;
import org.openqa.selenium.Alert;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
// Assume driver has navigated to the page and an action causes the alert.
Alert alert = driver.switchTo().alert();
alert.accept();
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
// Copy screenshot to your desired destination.
Use a destination your process can write to, and handle any file-copy errors separately from the browser capture.
Rank #2
Choose the right alert action
Selenium provides alert-specific operations because these dialogs are not DOM elements. The alert API exposes the message text and actions that depend on the dialog type.
- Alert: call
accept()to close the OK-style dialog. - Confirmation: call
accept()to confirm ordismiss()to cancel. - Prompt: use
send_keys()to enter text if needed, then accept or dismiss it.
For example, Python’s alert handle can be inspected before acceptance: alert.text. Don’t use prompt text entry for an ordinary alert.
Rank #3
Common failures and fixes
- No alert is present: the switch or wait may run before the page triggers the dialog, or the UI may not be a JavaScript alert. Wait for the expected alert after the triggering action; if it is an HTML overlay or browser permission prompt, it needs a different handling method.
- The wait times out: verify that the action which opens the alert actually ran and that the page reached the state that triggers it. Increase the timeout only if the expected operation legitimately takes longer.
- Screenshot call fails while a dialog is open: handle the alert before requesting the image. The required sequence is alert action first, screenshot second.
- Screenshot file is missing or cannot be saved: use a writable path and check the return value of Python’s
save_screenshot(), or handle file-copy and filesystem exceptions in Java. - Image omits content below the viewport: Selenium screenshots the current browsing context; full-page behavior depends on the browser and driver implementation. Check the relevant implementation rather than assuming the image includes the entire document.
Special case: beforeunload prompts
A beforeunload prompt is distinct from an alert triggered by page JavaScript. Selenium’s alert documentation says recent drivers automatically dismiss these prompts by default. Verify the behavior and applicable driver setting for the browser and driver versions you use before relying on it.
Or skip the browser setup
ScreenshotNeo is a website screenshot API and MCP server. A single GET request captures a URL as an image or PDF; it does not run your Selenium interaction or accept an alert on your behalf.
Rank #4
For pages that can be captured directly by URL, this cURL request saves a WebP screenshot:
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorscurl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
See the ScreenshotNeo documentation for request parameters and response details. Cookie banners, popups and chat widgets are removed before the shot; bot checks, blank pages and failed loads are never billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000.
Sign up for ScreenshotNeo’s free plan.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Frequently Asked Questions
Can Selenium accept an alert without finding it in the page DOM?
Yes. Use the WebDriver alert API, such as Python’s driver.switch_to.alert.accept(); alerts are native browser dialogs, not DOM elements.
Best Value
Does accepting an alert close a confirmation or prompt too?
Yes. accept() accepts an alert, confirms a confirmation dialog, or submits a prompt. A confirmation can instead be canceled with dismiss(), and a prompt can receive text with send_keys().
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Recommended Free Tools




