Move Python examples in Windows interactions docs#2629
Move Python examples in Windows interactions docs#2629itsveence wants to merge 4 commits intoSeleniumHQ:trunkfrom
Conversation
…st file Moves the inline Python examples for getting the current window handle and switching window into examples/python/tests/interactions/test_windows.py, and references it from the docs via gh-codeblock.
…o test file Moves the inline Python code examples for closing a window and switching to a new window into examples/python/tests/interactions/test_windows.py and references it from the docs via gh-codeblock.
👷 Deploy request for selenium-dev pending review.Visit the deploys page to approve it
|
Review Summary by QodoMove Python window examples to test file with translations
WalkthroughsDescription• Move Python window interaction examples from inline docs to test file • Replace inline code with gh-codeblock references in documentation • Add Japanese, Chinese, and Portuguese translations for windows docs • Create comprehensive test suite for window handling operations Diagramflowchart LR
A["Inline Python Code<br/>in Markdown"] -->|"Extract & Refactor"| B["Test File<br/>test_windows.py"]
B -->|"Reference via"| C["gh-codeblock<br/>Shortcode"]
C -->|"Update"| D["EN Documentation"]
D -->|"Translate to"| E["JA/ZH-CN/PT-BR<br/>Docs"]
File Changes1. examples/python/tests/interactions/test_windows.py
|
Code Review by Qodo
1. Missing window handle wait
|
| def test_switch_to_window(driver): | ||
| driver.get(url) | ||
| original_window_handles = set(driver.window_handles) | ||
| assert len(original_window_handles) == 1 | ||
| driver.find_element(By.LINK_TEXT, "Open new window").click() | ||
| new_handles = set(driver.window_handles) - original_window_handles | ||
| assert len(new_handles) == 1 |
There was a problem hiding this comment.
1. Missing window handle wait 🐞 Bug ☼ Reliability
test_switch_to_window clicks “Open new window” and immediately reads driver.window_handles, which can race and intermittently produce no new handle (flaky CI failures). The file already imports WebDriverWait/expected_conditions but never uses them.
Agent Prompt
### Issue description
The Python window-switching example is race-prone: it clicks to open a new window and immediately inspects `driver.window_handles`, which can be stale right after the click.
### Issue Context
These examples are executed in CI (`pytest ... -n auto --reruns 3`), so timing races can cause intermittent failures. The file already imports `WebDriverWait` and `expected_conditions` but doesn’t use them.
### Fix Focus Areas
- examples/python/tests/interactions/test_windows.py[21-30]
### Suggested change
Add an explicit wait after the click, e.g.:
- `wait = WebDriverWait(driver, 10)`
- `wait.until(EC.number_of_windows_to_be(2))`
Then compute the new handle and switch. This will also make the existing `WebDriverWait`/`EC` imports used (or remove them if you choose a different waiting strategy).
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| def test_close_window(driver): | ||
| driver.get(url) | ||
| driver.find_element(By.LINK_TEXT, "Open new window").click() | ||
| driver.close() | ||
| current_handles = driver.window_handles | ||
| assert len(current_handles) == 1 |
There was a problem hiding this comment.
2. Close without switching back 🐞 Bug ≡ Correctness
test_close_window closes the current window and continues without switching to a remaining valid handle, which matches the documented anti-pattern that triggers NoSuchWindow on subsequent commands. This also leaves the docs with a Python close example that doesn’t demonstrate the required switch-back step.
Agent Prompt
### Issue description
The Python close-window example closes a window but does not switch WebDriver back to an existing window handle, contradicting the page’s guidance and demonstrating a pattern that can throw `NoSuchWindow`.
### Issue Context
The Windows interactions documentation explicitly warns that failing to switch after closing will leave WebDriver executing on a closed page.
### Fix Focus Areas
- examples/python/tests/interactions/test_windows.py[32-37]
### Suggested change
Update `test_close_window` to follow the documented flow:
1. Store `original_window = driver.current_window_handle`
2. Open the new window and (optionally) wait for 2 windows
3. Switch to the new window, then `driver.close()`
4. Switch back to `original_window` (or the remaining handle)
This will also let the docs embed a complete close+switch example.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
Description
Moves the inline python examples for Windows interactions page to
examples/python/tests/interactions/test_windows.py:These are now referenced via the
gh-codeblockshortcode.Motivation and Context
Part of ongoing effort to run all code examples in the CI by moving all inline code examples out of the documentation Markdown, as described in the "Moving examples" section of the contributing guide
Types of changes
Checklist