Replace implicit lambda workaround with EC.alert_is_present in Python alert examples (Fixes #2548)#2589
Conversation
Replace implicit lambda workaround with purpose-built expected condition for alert handling in all three Python alert examples. Fixes SeleniumHQ#2548
👷 Deploy request for selenium-dev pending review.Visit the deploys page to approve it
|
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
|||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
|||||||||
There was a problem hiding this comment.
Since this adds an import and shifts the line numbers, the codeblocks in the example page need to be adjusted to match.
The line numbers that need to be changed are in:
website_and_docs/content/documentation/webdriver/interactions/alerts.en.md (and alerts.ja.md, alerts.pt-br.md, alerts.zh-cn.md)
Other than that, I think this change is fine.
| alert.accept() | ||
| assert text == "Sample alert" | ||
|
|
||
|
@beinghumantester please make the suggested changes and update your branch if you want this merged. |
User description
Description
Replaces the implicit lambda-based alert wait with the purpose-built
EC.alert_is_present()expected condition in all three Python alert examples.The previous implementation used:
This has been updated to:
Motivation and Context
The examples previously used a lambda workaround to wait for alerts. While functional, Selenium provides a dedicated expected condition specifically designed for this use case.
Using
EC.alert_is_present():All three Python alert examples have been updated: Alert, Confirm, and Prompt.
Files Reviewed but Intentionally Not Changed
test_expected_conditions.pyContains
wait.until(lambda _ : revealed.is_displayed())— left unchanged because no EC equivalent exists for an already-located element.EC.visibility_of_element_located()requires a locator tuple, not an existingWebElement.test_waits.pyContains two lambdas, both intentionally left unchanged:
lambda _ : revealed.is_displayed()— same reasoning as abovelambda _ : revealed.send_keys("Displayed") or True— custom condition that performs an action and returnsTrue. No direct EC equivalent exists for this pattern.Types of Changes
Checklist
PR Type
Enhancement
Description
Replace lambda workaround with
EC.alert_is_present()in Python alert examplesAdd import for
expected_conditionsmoduleImproves code readability and aligns with Selenium best practices
Updates all three alert test cases (Alert, Confirm, Prompt)
Diagram Walkthrough
File Walkthrough
test_alerts.py
Replace lambda alert waits with EC.alert_is_present()examples/python/tests/interactions/test_alerts.py
expected_conditionsmodule fromselenium.webdriver.supportEC.alert_is_present()intest_alert_popup()EC.alert_is_present()intest_confirm_popup()EC.alert_is_present()intest_prompt_popup()