在页面跳转之前用:
current_window = firefox_login.current_window_handle # 获取当前窗口handle name
跳转之后先获取到页面的全部handle,在进行动作:
all_window=firefox_login.window_handles
for window in all_window:
if window != current_window:
firefox_logiEXAMPLE:
firefox_login=webdriver.Firefox()
firefox_login.get('') #填入URL
firefox_login.find_element_by_xpath('').clear()
firefox_login.find_element_by_xpath('').send_keys() #'填入账号'
firefox_login.find_element_by_xpath('').clear()
firefox_login.find_element_by_xpath('').send_keys() #'填入密码'
current_window = firefox_login.current_window_handle # 获取当前窗口handle name
firefox_login.find_element_by_xpath('//*[@id="loginbtn"]').click()
all_window=firefox_login.window_handles
for window in all_window:
if window != current_window:
firefox_login.switch_to.window(window)
current_window = firefox_login.current_window_handle # 获取当前窗口handle name
firefox_login.find_element_by_xpath('').click()
n.switch_to.window(window)