方法:
(1)driver.current_window_handle 获取当前窗口句柄
(2)driver.window_handles 获取所有窗口句柄
(3)driver.switch_to.window(handle) 切换指定句柄窗口
(4)driver.close() 关闭当前的句柄
(5)driver.quit() 关闭浏览器
例:
from selenium import webdriver import time d = webdriver.Firefox() d.get('https://www.baidu.com/') d.find_element_by_id('kw').clear() d.find_element_by_id('kw').send_keys("慕课网") d.find_element_by_id('su').click() time.sleep(10) d.find_element_by_partial_link_text('程序员的梦工厂').click() time.sleep(10) print(d.window_handles) d.switch_to.window(d.window_handles[1]) print(d.current_window_handle) print(d.current_url) d.close() d.quit()
输出:
['6442450945', '6442450949'] 6442450949 https://www.imooc.com/
完