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() #關閉所有關聯的窗口

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM