seleniumhq.github.io icon indicating copy to clipboard operation
seleniumhq.github.io copied to clipboard

JavaScript alerts, prompts and confirmations - Non-working (?) example in python

Open kjo-sdds opened this issue 1 month ago • 1 comments

It seems that the example in Python (can't speak for the other languages, I'm only using python) is not the best suited :

    wait = WebDriverWait(driver, timeout=2)
    alert = wait.until(lambda d : d.switch_to.alert)
    text = alert.text
    alert.accept()

Seems to ONLY work if the alert is already present at the time the code runs. Which :

  1. Does not really correspond to the semantic of wait.until
  2. Does not work if the alert is generated on the return of an async function

Especially it seems there's a way made specifically for that :

from selenium.webdriver.support import expected_conditions as EC

# [...]

        wait = WebDriverWait(driver, timeout=4)
        alert = wait.until(EC.alert_is_present())
        text = alert.text
        alert.accept()

Maybe I missed it (then it's just me the moron) ... but kind of a bummer that I had to find it elsewhere than the docs ...

kjo-sdds avatar Dec 05 '25 16:12 kjo-sdds

noticed in other programming language examples expected conditions is alert present is used, but observation about python seems correct. if you would like to contribute and improve the example in documentation, please do.

rpallavisharma avatar Dec 09 '25 11:12 rpallavisharma