Skip to content

Replace implicit lambda workaround with EC.alert_is_present in Python alert examples (Fixes #2548)#2589

Open
beinghumantester wants to merge 1 commit intoSeleniumHQ:trunkfrom
beinghumantester:issue-2548-use-ec-alert-is-present
Open

Replace implicit lambda workaround with EC.alert_is_present in Python alert examples (Fixes #2548)#2589
beinghumantester wants to merge 1 commit intoSeleniumHQ:trunkfrom
beinghumantester:issue-2548-use-ec-alert-is-present

Conversation

@beinghumantester
Copy link
Contributor

@beinghumantester beinghumantester commented Feb 22, 2026

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:

wait.until(lambda d: d.switch_to.alert)

This has been updated to:

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

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

  • Improves readability and makes intent explicit
  • Aligns Python examples with official Selenium best practices
  • Makes the code clearer for users learning alert handling
  • Consistent with how other languages handle alert waiting in the docs

All three Python alert examples have been updated: Alert, Confirm, and Prompt.

Files Reviewed but Intentionally Not Changed

test_expected_conditions.py

Contains 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 existing WebElement.

test_waits.py

Contains two lambdas, both intentionally left unchanged:

  • lambda _ : revealed.is_displayed() — same reasoning as above
  • lambda _ : revealed.send_keys("Displayed") or True — custom condition that performs an action and returns True. No direct EC equivalent exists for this pattern.

Types of Changes

  • Change to the site (I have double-checked the Netlify deployment, and my changes look good)

Checklist

  • I have read the contributing document
  • I have used Hugo to render the site/docs locally and verified the changes
Screenshot 2026-02-22 151029

PR Type

Enhancement


Description

  • Replace lambda workaround with EC.alert_is_present() in Python alert examples

  • Add import for expected_conditions module

  • Improves code readability and aligns with Selenium best practices

  • Updates all three alert test cases (Alert, Confirm, Prompt)


Diagram Walkthrough

flowchart LR
  A["Lambda workaround<br/>wait.until(lambda d: d.switch_to.alert)"] -- "Replace with" --> B["EC.alert_is_present()<br/>wait.until(EC.alert_is_present())"]
  C["Import added<br/>expected_conditions as EC"] --> B
  B --> D["Three test cases updated<br/>Alert, Confirm, Prompt"]
Loading

File Walkthrough

Relevant files
Enhancement
test_alerts.py
Replace lambda alert waits with EC.alert_is_present()       

examples/python/tests/interactions/test_alerts.py

  • Added import for expected_conditions module from
    selenium.webdriver.support
  • Replaced lambda-based alert wait with EC.alert_is_present() in
    test_alert_popup()
  • Replaced lambda-based alert wait with EC.alert_is_present() in
    test_confirm_popup()
  • Replaced lambda-based alert wait with EC.alert_is_present() in
    test_prompt_popup()
+5/-4     

Replace implicit lambda workaround with purpose-built expected
condition for alert handling in all three Python alert examples.
Fixes SeleniumHQ#2548
@netlify
Copy link

netlify bot commented Feb 22, 2026

👷 Deploy request for selenium-dev pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 3ead74b

@qodo-code-review
Copy link
Contributor

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
🟢
No security concerns identified No security vulnerabilities detected by AI analysis. Human verification advised for critical code.
Ticket Compliance
🎫 No ticket provided
  • Create ticket/issue
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
🟢
Generic: Comprehensive Audit Trails

Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Meaningful Naming and Self-Documenting Code

Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Robust Error Handling and Edge Case Management

Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Error Handling

Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Logging Practices

Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Security-First Input Validation and Data Handling

Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

@qodo-code-review
Copy link
Contributor

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Always ensure driver quits

Wrap the browser lifecycle logic in a try...finally block to ensure
driver.quit() is always called, preventing orphaned browser processes if a test
fails.

examples/python/tests/interactions/test_alerts.py [11-22]

 driver = webdriver.Chrome()
-driver.get(url)
-...
-driver.quit()
+try:
+    driver.get(url)
+    ...
+finally:
+    driver.quit()

[To ensure code accuracy, apply this suggestion manually]

Suggestion importance[1-10]: 8

__

Why: This is a crucial improvement for test stability, as it correctly identifies that a test failure would leave an orphaned browser process, and suggests a standard try...finally block to ensure proper resource cleanup.

Medium
  • More

@VietND96 VietND96 requested a review from cgoldberg February 24, 2026 23:39
Copy link
Member

@cgoldberg cgoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this whitespace.

@cgoldberg
Copy link
Member

@beinghumantester please make the suggested changes and update your branch if you want this merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants