I'm working with a web page in Selenium. Clicking on one link opens another window and I want to transfer control to that window.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://www.tagindex.com/html_tag/link/a_target.html')
driver.find_element_by_css_selector('[href="target_example.html"]').click()
driver.switch_to.window(driver.window_handles[1])
However, this code may fail with ʻIndexError without
driver.window_handles [1] `existing: sob:
By using WebDriverWait.until () until another window opens You can wait.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
driver.get('http://www.tagindex.com/html_tag/link/a_target.html')
driver.find_element_by_css_selector('[href="target_example.html"]').click()
WebDriverWait(driver, 3).until(lambda d: len(d.window_handles) > 1)
driver.switch_to.window(driver.window_handles[1])
This ensures that control can be transferred to another window: blush:
Recommended Posts