1. 程式人生 > >多窗口切換

多窗口切換

sele 所有 nal all text switch 分享 ive port

# 多窗口切換
# current_window_handle :獲得當前窗口句柄
# window_handles :返回所有窗口的句柄到當前會話上
# switch_to_window : 用於切換到不同的窗口
from selenium import webdriver
from time import sleep
url = https://www.baidu.com/

def win_swith():
    driver = webdriver.Chrome()
    driver.implicitly_wait(10)
    driver.get(url)
    win1 
= driver.current_window_handle driver.find_element_by_link_text(登錄).click() sleep(3) driver.find_element_by_link_text(立即註冊).click() sleep(5) winall = driver.window_handles driver.switch_to_window(win1) sleep(3) a1 = driver.current_url print(a1) driver.switch_to_window(winall[
1]) sleep(3) a2 = driver.current_url print(a2) driver.switch_to_window(winall[0]) sleep(3) a3 = driver.current_url print(a3) sleep(3) driver.quit() if __name__ == __main__: win_swith()
技術分享圖片

多窗口切換