premise:
During the operation page sometimes click a link will pop up a new window, which requires the host to the switch on a newly opened window. Providing WebDriver switch_to.window () method may be implemented to switch between different windows.
content:
In Baidu, for example, switching between the two windows:
from Selenium the webdriver Import Import Time Driver = webdriver.Chrome () driver.implicitly_wait ( 10 ) Driver. GET ( " http://www.baidu.com " ) # Baidu search window obtained sreach_windows = driver.current_window_handle driver.find_element_by_link_text ( ' Login ' ) .click () driver.find_element_by_link_text ( " Register Now " ) .click () # get all currently open window handle all_handles = driver.window_handles # to enter the registration window for handle in all_handles: if handle != sreach_windows: driver.switch_to.window(handle) print('now register window!') driver.find_element_by_name("account").send_keys('username') driver.find_element_by_name('password').send_keys('password') time.sleep(2) driver.quit()
Is to first save the current window tag, then kept behind open windows to circulate, to jump to the window you want.