Skip to content

Move Python examples in Windows interactions docs#2629

Open
itsveence wants to merge 4 commits intoSeleniumHQ:trunkfrom
itsveence:move-windows-python-example
Open

Move Python examples in Windows interactions docs#2629
itsveence wants to merge 4 commits intoSeleniumHQ:trunkfrom
itsveence:move-windows-python-example

Conversation

@itsveence
Copy link
Copy Markdown

Description

Moves the inline python examples for Windows interactions page to examples/python/tests/interactions/test_windows.py:

  • Get window handle
  • Switching windows or tabs
  • Closing a window or tab
  • Create new window (or) new tab and switch
  • Quitting the browser at the end of a session

These are now referenced via the gh-codeblock shortcode.

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

  • 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.

…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.
@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 29, 2026

👷 Deploy request for selenium-dev pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 33f688b

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 29, 2026

CLA assistant check
All committers have signed the CLA.

@qodo-code-review
Copy link
Copy Markdown
Contributor

Review Summary by Qodo

Move Python window examples to test file with translations

✨ Enhancement 📝 Documentation

Grey Divider

Walkthroughs

Description
• 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
Diagram
flowchart 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"]
Loading

Grey Divider

File Changes

1. examples/python/tests/interactions/test_windows.py 🧪 Tests +42/-0

Add comprehensive window interaction test suite

• Added pytest fixture for Chrome WebDriver setup and teardown
• Implemented test_current_window_handle() to verify window handle retrieval
• Implemented test_switch_to_window() to test window switching functionality
• Implemented test_close_window() to test closing windows
• Implemented test_new_window() to test creating new tabs and windows

examples/python/tests/interactions/test_windows.py


2. website_and_docs/content/documentation/webdriver/interactions/windows.en.md 📝 Documentation +12/-46

Replace inline Python code with gh-codeblock references

• Replaced inline Python code snippets with gh-codeblock references
• Updated current window handle example to reference test file lines 16-18
• Updated switch to window example to reference test file lines 22-30
• Updated close window example to reference test file line 35
• Updated new window example to reference test file lines 40-43
• Updated quit example to reference test file line 13

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


3. website_and_docs/content/documentation/webdriver/interactions/windows.ja.md 📝 Documentation +12/-47

Update Japanese docs with gh-codeblock references

• Replaced inline Python code snippets with gh-codeblock references
• Updated all five window interaction examples to reference test file
• Maintained Japanese translation content while updating code references

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


View more (2)
4. website_and_docs/content/documentation/webdriver/interactions/windows.pt-br.md 📝 Documentation +12/-47

Update Portuguese docs with gh-codeblock references

• Replaced inline Python code snippets with gh-codeblock references
• Updated all five window interaction examples to reference test file
• Maintained Portuguese translation content while updating code references

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


5. website_and_docs/content/documentation/webdriver/interactions/windows.zh-cn.md 📝 Documentation +12/-47

Update Chinese docs with gh-codeblock references

• Replaced inline Python code snippets with gh-codeblock references
• Updated all five window interaction examples to reference test file
• Maintained Chinese translation content while updating code references

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


Grey Divider

Qodo Logo

@qodo-code-review
Copy link
Copy Markdown
Contributor

qodo-code-review Bot commented Apr 29, 2026

Code Review by Qodo

🐞 Bugs (2) 📘 Rule violations (0)

Grey Divider


Action required

1. Missing window handle wait 🐞 Bug ☼ Reliability
Description
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.
Code

examples/python/tests/interactions/test_windows.py[R21-27]

+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
Evidence
The new test reads window handles immediately after the click with no wait, even though the
documentation section shows other languages explicitly waiting for the second window handle to exist
before switching. The presence of unused WebDriverWait/EC imports further indicates the wait
step was intended but omitted.

examples/python/tests/interactions/test_windows.py[1-5]
examples/python/tests/interactions/test_windows.py[21-30]
website_and_docs/content/documentation/webdriver/interactions/windows.en.md[36-72]

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 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


2. Close without switching back 🐞 Bug ≡ Correctness
Description
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.
Code

examples/python/tests/interactions/test_windows.py[R32-37]

+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
Evidence
The test closes a window but never calls switch_to.window(...) afterwards. The Windows docs
explicitly say you must switch back after closing or you’ll hit a No Such Window Exception, and the
Java example does switch back after close().

examples/python/tests/interactions/test_windows.py[32-37]
website_and_docs/content/documentation/webdriver/interactions/windows.en.md[140-186]
examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java[31-34]

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 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


Grey Divider

Qodo Logo

Comment on lines +21 to +27
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
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.

Action required

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

Comment on lines +32 to +37
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
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.

Action required

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

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