Skip to content

Use EC.alert_is_present() for Python alert examples#2730

Closed
diemol wants to merge 3 commits into
trunkfrom
fix-alerts-ec-present
Closed

Use EC.alert_is_present() for Python alert examples#2730
diemol wants to merge 3 commits into
trunkfrom
fix-alerts-ec-present

Conversation

@diemol

@diemol diemol commented Jul 18, 2026

Copy link
Copy Markdown
Member

Thanks for contributing to the Selenium site and documentation!
A PR well described will help maintainers to review and merge it quickly

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, and help reviewers by making them as simple and short as possible.

Description

Picks up #2589 (by @beinghumantester): replaces wait.until(lambda d: d.switch_to.alert) with wait.until(EC.alert_is_present()) in all three Python alert examples, and addresses the review feedback that PR didn't get to: removes the trailing whitespace cgoldberg flagged, and fixes the gh-codeblock line ranges in alerts.en.md, alerts.ja.md, alerts.pt-br.md, and alerts.zh-cn.md, which had drifted to include driver.get(url) and the assert line instead of matching the same scope shown before (and in the other language tabs).

Motivation and Context

WebDriverWait only ignores NoSuchElementException by default, not NoAlertPresentException, so the old lambda would raise instead of retrying if the alert hadn't appeared yet on the first poll. EC.alert_is_present() catches that exception internally, so the wait actually retries as intended.

Types of changes

  • Change to the site (I have double-checked the Netlify deployment, and my changes look good)
  • Code example added (and I also added the example to all translated languages)
  • Improved translation
  • Added new translation (and I also added a notice to each document missing translation)

Checklist

  • I have read the contributing document.
  • I have used hugo to render the site/docs locally and I am sure it works.

beinghumantester and others added 3 commits July 19, 2026 00:01
Replace implicit lambda workaround with purpose-built expected
condition for alert handling in all three Python alert examples.
Fixes #2548
Removes the trailing whitespace cgoldberg flagged on PR #2589, and
corrects the gh-codeblock line ranges in alerts.en.md, alerts.ja.md,
alerts.pt-br.md, and alerts.zh-cn.md. The prior update shifted each
range by widening both ends instead of moving by the one line the new
import adds, so the rendered Python snippets pulled in driver.get(url)
and the assert line that the other language tabs intentionally omit.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@qodo-code-review

Copy link
Copy Markdown
Contributor

PR Summary by Qodo

Use EC.alert_is_present() in Python alert examples and align docs snippets

🐞 Bug fix 📝 Documentation 🕐 10-20 Minutes

Grey Divider

AI Description

• Fix Python alert waits by using EC.alert_is_present() to safely retry until an alert exists.
• Add the missing expected_conditions import in the shared Python alerts example.
• Correct gh-codeblock line ranges so translated alert docs render consistent Python snippets.
Diagram

graph TD
  A["Alert docs (multi-locale)"] --> B["Hugo gh-codeblock"] --> C["test_alerts.py"] --> D["WebDriverWait"] --> E["EC.alert_is_present()"] --> F["Browser alert"]
Loading
High-Level Assessment

Using EC.alert_is_present() is the most idiomatic and reliable approach because it handles NoAlertPresentException internally, enabling WebDriverWait polling to work as intended. Alternatives like a try/except-wrapped lambda or configuring ignored_exceptions would be more verbose and easier to get subtly wrong.

Files changed (5) +16 / -15

Bug fix (1) +4 / -3
test_alerts.pyUse EC.alert_is_present() for alert waits in all Python examples +4/-3

Use EC.alert_is_present() for alert waits in all Python examples

• Imports Selenium expected_conditions as EC and replaces the lambda-based alert retrieval with EC.alert_is_present() in the alert, confirm, and prompt examples. This makes the wait resilient to NoAlertPresentException so polling retries correctly.

examples/python/tests/interactions/test_alerts.py

Documentation (4) +12 / -12
alerts.en.mdFix Python gh-codeblock line ranges for alert snippets (EN) +3/-3

Fix Python gh-codeblock line ranges for alert snippets (EN)

• Adjusts the referenced line ranges so the embedded Python snippets match the intended scope after the import change. Prevents the rendered snippet from drifting to include extra setup/assert lines.

website_and_docs/content/documentation/webdriver/interactions/alerts.en.md

alerts.ja.mdFix Python gh-codeblock line ranges for alert snippets (JA) +3/-3

Fix Python gh-codeblock line ranges for alert snippets (JA)

• Updates the line ranges for the three embedded Python alert snippets to align with the canonical example after the import shift. Keeps the Japanese page consistent with other language tabs.

website_and_docs/content/documentation/webdriver/interactions/alerts.ja.md

alerts.pt-br.mdFix Python gh-codeblock line ranges for alert snippets (PT-BR) +3/-3

Fix Python gh-codeblock line ranges for alert snippets (PT-BR)

• Corrects the Python snippet line ranges so the rendered examples exclude unintended surrounding lines. Maintains parity with the other locale tabs’ snippet boundaries.

website_and_docs/content/documentation/webdriver/interactions/alerts.pt-br.md

alerts.zh-cn.mdFix Python gh-codeblock line ranges for alert snippets (ZH-CN) +3/-3

Fix Python gh-codeblock line ranges for alert snippets (ZH-CN)

• Shifts the Python snippet line ranges to match the updated example file and avoid including extra context lines. Ensures the Chinese documentation renders the same snippet scope as other locales.

website_and_docs/content/documentation/webdriver/interactions/alerts.zh-cn.md

@netlify

netlify Bot commented Jul 18, 2026

Copy link
Copy Markdown

Deploy Preview for selenium-dev ready!

Name Link
🔨 Latest commit 3db0791
🔍 Latest deploy log https://app.netlify.com/projects/selenium-dev/deploys/6a5bf815f071000008a96be5
😎 Deploy Preview https://deploy-preview-2730--selenium-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@qodo-code-review

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📜 Skill insights (0)

Context used
✅ Compliance rules (platform): 10 rules

Grey Divider


Remediation recommended

1. Docs snippet lacks EC import 🐞 Bug ≡ Correctness
Description
The alert docs embed Python code ranges that include wait.until(EC.alert_is_present()), but those
ranges exclude the expected_conditions as EC import that defines EC, so the displayed snippet
has an unresolved identifier for readers copying it.
Code

website_and_docs/content/documentation/webdriver/interactions/alerts.en.md[33]

+{{< gh-codeblock path="/examples/python/tests/interactions/test_alerts.py#L13-L19" >}}
Evidence
The docs embed /examples/python/tests/interactions/test_alerts.py#L13-L19, which includes
EC.alert_is_present() (line 17), but the only place EC is defined is the import at line 4, which
is outside the embedded range.

website_and_docs/content/documentation/webdriver/interactions/alerts.en.md[27-35]
website_and_docs/content/documentation/webdriver/interactions/alerts.ja.md[23-31]
website_and_docs/content/documentation/webdriver/interactions/alerts.pt-br.md[27-33]
website_and_docs/content/documentation/webdriver/interactions/alerts.zh-cn.md[20-27]
examples/python/tests/interactions/test_alerts.py[1-20]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The Python alert examples shown via `gh-codeblock` now include `EC.alert_is_present()` but do not show the import that defines `EC`, making the displayed snippet contain an undefined identifier.

## Issue Context
`EC` is introduced only by `from selenium.webdriver.support import expected_conditions as EC` in the source test file, but the doc pages embed only mid-file line ranges that start at the interaction code.

## Fix Focus Areas
Update the docs so that the Python snippet either (a) includes the import lines in the embedded range, or (b) adds a small explicit import snippet/note adjacent to the codeblock in each language page.

- website_and_docs/content/documentation/webdriver/interactions/alerts.en.md[27-35]
- website_and_docs/content/documentation/webdriver/interactions/alerts.ja.md[23-31]
- website_and_docs/content/documentation/webdriver/interactions/alerts.pt-br.md[27-33]
- website_and_docs/content/documentation/webdriver/interactions/alerts.zh-cn.md[20-27]
- examples/python/tests/interactions/test_alerts.py[1-20]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Qodo Logo


{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/interactions/test_alerts.py#L12-L18" >}}
{{< gh-codeblock path="/examples/python/tests/interactions/test_alerts.py#L13-L19" >}}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remediation recommended

1. Docs snippet lacks ec import 🐞 Bug ≡ Correctness

The alert docs embed Python code ranges that include wait.until(EC.alert_is_present()), but those
ranges exclude the expected_conditions as EC import that defines EC, so the displayed snippet
has an unresolved identifier for readers copying it.
Agent Prompt
## Issue description
The Python alert examples shown via `gh-codeblock` now include `EC.alert_is_present()` but do not show the import that defines `EC`, making the displayed snippet contain an undefined identifier.

## Issue Context
`EC` is introduced only by `from selenium.webdriver.support import expected_conditions as EC` in the source test file, but the doc pages embed only mid-file line ranges that start at the interaction code.

## Fix Focus Areas
Update the docs so that the Python snippet either (a) includes the import lines in the embedded range, or (b) adds a small explicit import snippet/note adjacent to the codeblock in each language page.

- website_and_docs/content/documentation/webdriver/interactions/alerts.en.md[27-35]
- website_and_docs/content/documentation/webdriver/interactions/alerts.ja.md[23-31]
- website_and_docs/content/documentation/webdriver/interactions/alerts.pt-br.md[27-33]
- website_and_docs/content/documentation/webdriver/interactions/alerts.zh-cn.md[20-27]
- examples/python/tests/interactions/test_alerts.py[1-20]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

@diemol

diemol commented Jul 18, 2026

Copy link
Copy Markdown
Member Author

Superseded — the fix (plus the review feedback) has been pushed directly onto #2589 instead. Closing this one.

@diemol diemol closed this Jul 18, 2026
@diemol
diemol deleted the fix-alerts-ec-present branch July 18, 2026 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants