窗口切换:
当前窗口句柄 current_window_handle
所有的窗口句柄 window_handles
切换窗口 switch_to_window()
#coding=utf-8 #切换窗口 from selenium import webdriver import time dr = webdriver.Firefox() dr.get("http://www.baidu.com") dr.find_element_by_link_text("登录").click() time.sleep(1) dr.find_element_by_link_text("立即注册").click() origin_handle = dr.current_window_handle #保存当前窗口 print('当前窗口title:',dr.title) handles = dr.window_handles #获取所有窗口 for handle in handles: if handle != origin_handle: dr.switch_to_window(handle) time.sleep(1) print('当前窗口title:',dr.title) #切换回原窗口 dr.switch_to_window(origin_handle) print('当前窗口title:',dr.title) time.sleep(1) dr.quit()