selenium多窗口切换,及其关闭
# -*- coding:utf-8 -*- from selenium import webdriver import time from selenium.webdriver.common.by import By driver = webdriver.Chrome(executable_path="chromedriver.exe") driver.get("https://www.baidu.com") selenium_index = driver.current_window_handle #获取当前窗口的句柄 driver.find_element(By.LINK_TEXT,"新闻").click() time.sleep(3) driver.switch_to.window(selenium_index)#跳转 time.sleep(3) driver.find_element(By.LINK_TEXT,"直播").click() time.sleep(2) driver.close() handles = driver.window_handles #获取所有的 handles print(handles) # for handle in handles: # driver.switch_to.window(handle) # time.sleep(1) # time.sleep(2) driver.quit() #关闭所有关联的窗口