18. Window Handles & Multi-Tab Switching in Python

Switching Windows & Tabs

parent_handle = driver.current_window_handle

driver.find_element(By.ID, "open-tab-btn").click()

for handle in driver.window_handles:
if handle != parent_handle:
driver.switch_to.window(handle)
break

print("New tab title:", driver.title)
driver.close()
driver.switch_to.window(parent_handle)

Task

  • Implement switch_to_new_tab(driver, parent_handle):
  • 1. Iterate driver.window_handles.
  • 2. If handle != parent_handle, driver.switch_to.window(handle) and return True.
  • 3. Return False if no new tab found.
πŸ’‘Common doubts
Did you like the problem?
Lesson 18 of 43
Python
Loading code editor…
Next