SeleniumHQ / seleniumhq.github.io

Official Selenium website and documentation
https://selenium.dev
Apache License 2.0
1.12k stars 1.31k forks source link

[🐛 Bug]: Python - JavaScript alerts, prompts and confirmations #1333

Closed paulotcj closed 2 months ago

paulotcj commented 1 year ago

What happened?

In the documentation, the first example for handling alerts in Python at the address: https://www.selenium.dev/documentation/webdriver/interactions/alerts/ is the following:

`driver.find_element(By.LINK_TEXT, "See an example alert").click()

alert = wait.until(expected_conditions.alert_is_present())

text = alert.text alert.accept()`

However, this code will fail. The probable solution as the example below hints, should be: `driver.find_element(By.LINK_TEXT, "See an example alert").click()

WebDriverWait(driver, 10).until(expected_conditions.alert_is_present())

alert = driver.switch_to.alert

alert.accept()`

My apologies if I am missing something here.

What browsers and operating systems are you seeing the problem on?

Any

github-actions[bot] commented 1 year ago

@paulotcj, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

titusfortner commented 1 year ago

Hmm, I think the expected condition does switch to the alert?

At any rate, we are trying to update all code examples to actual code in the examples directory of this repo so that we can ensure things are properly passing. (https://www.selenium.dev/documentation/about/style/#reference-github-examples)

Is there any chance you'd be interested in writing that code in: https://github.com/SeleniumHQ/seleniumhq.github.io/tree/trunk/examples/python/tests/interactions/test_alerts.py so that we can see it passing? We want all code examples using code in selenium.dev, so alerts tests should use: https://www.selenium.dev/selenium/web/alerts.html

eaccmk commented 1 year ago

Hi @paulotcj , @titusfortner , This issue will mostly be covered by https://github.com/SeleniumHQ/seleniumhq.github.io/pull/1470

shbenzer commented 2 months ago

Can be closed. Current python code example is as follows:

element = driver.find_element(By.LINK_TEXT, "See an example alert")
element.click()
wait = WebDriverWait(driver, timeout=2)
alert = wait.until(lambda d : d.switch_to.alert)
text = alert.text
alert.accept()
diemol commented 2 months ago

Thank you, @shbenzer!