Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk4 min

How to Capture a Page Screenshot with Selenium After Accepting a JavaScript Alert

Wait for or switch to Selenium’s JavaScript alert, accept it, and then capture the current browsing context. Examples and fixes for Python and Java.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Alert: call accept() to close the OK-style dialog.
  • Confirmation: call accept() to confirm or dismiss() 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.

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.

For pages that can be captured directly by URL, this cURL request saves a WebP screenshot:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -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.Support on Ko-Fi

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.

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().

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.