📜  如何在Selenium for Python切换到新窗口?

📅  最后修改于: 2022-05-13 01:55:22.446000             🧑  作者: Mango

如何在Selenium for Python切换到新窗口?

Selenium是用于 Web 测试和自动化的最常用的Python工具。但是当涉及到用户需要在 chrome 中的窗口之间切换的场景时,就会出现问题。因此,selenium也得到了保护。 Selenium使用这些方法来完成这项任务——

  • window_handles用于处理不同的窗口。它存储用于切换的窗口 ID。
  • switch_to.window方法用于在 window_handles id 的帮助下在窗口之间切换。

覆盖步骤:

  • 设置 URL 并导入selenium
Python3
# import modules
from selenium import webdriver  
import time  
  
# provide the path for chromedriver
PATH = "C:/chromedriver.exe"  
  
# pass on the path to driver for working
driver = webdriver.Chrome(PATH)


Python3
# Open login yahoo for sample url
driver.get("https://login.yahoo.com/")  
  
# print title of yahoo window
print("First window title = " + driver.title)
  
# Clicks on privacy and it opens in new window
driver.find_element_by_class_name("privacy").click()
  
# switch window in 7 seconds
time.sleep(7)  
  
# window_handles[1] is a second window
driver.switch_to.window(driver.window_handles[1])
  
# prints the title of the second window
print("Second window title = " + driver.title)
  
# window_handles[0] is a first window
driver.switch_to.window(driver.window_handles[0])
  
# prints windows id
print(driver.window_handles)


Python3
# Open the new window
driver.execute_script("window.open()")
  
# window switch to new 3rd window
driver.switch_to.window(driver.window_handles[2])
  
# get the new url in window 3
driver.get("https://www.geeksforgeeks.org/")
  
# print the 3rd window title
print(driver.title)


Python3
# import modules
from selenium import webdriver
import time
  
# assign path and driver
PATH = "C:/chromedriver.exe"
driver = webdriver.Chrome(PATH)
  
# assign URL
driver.get("https://login.yahoo.com/")
print("First window title = " + driver.title)
  
# switch to new window
driver.find_element_by_class_name("privacy").click()
print(driver.window_handles)
driver.switch_to.window(driver.window_handles[1])
print("Second window title = " + driver.title)
  
# switch to new window
driver.execute_script("window.open()")
print(driver.window_handles)
driver.switch_to.window(driver.window_handles[2])
driver.get("https://www.geeksforgeeks.org/")
print(driver.title)


  • 获取网站 URL 并单击在新窗口中打开的链接。并在它们之间切换。

蟒蛇3

# Open login yahoo for sample url
driver.get("https://login.yahoo.com/")  
  
# print title of yahoo window
print("First window title = " + driver.title)
  
# Clicks on privacy and it opens in new window
driver.find_element_by_class_name("privacy").click()
  
# switch window in 7 seconds
time.sleep(7)  
  
# window_handles[1] is a second window
driver.switch_to.window(driver.window_handles[1])
  
# prints the title of the second window
print("Second window title = " + driver.title)
  
# window_handles[0] is a first window
driver.switch_to.window(driver.window_handles[0])
  
# prints windows id
print(driver.window_handles)  

输出:



First window title = Yahoo
Second window title = Welcome to the Verizon Media Privacy Policy | Verizon Media Policies
['CDwindow-F25D48D2602CBD780FB2BE8B34A3BEAC', 'CDwindow-A80A74DFF7CCD47F628AF860F3D46913']

解释:

该程序将首先打开雅虎,然后在新选项卡中打开雅虎隐私,然后在 7 秒内再次切换回雅虎选项卡,即第一个窗口。

可选案例:如果用户需要打开一个新窗口并在它们之间切换。

  • execute_script是一种将 JavaScript 作为字符串传递的方法
  • window.open()是打开新窗口的方法

示例 1:

蟒蛇3

# Open the new window
driver.execute_script("window.open()")
  
# window switch to new 3rd window
driver.switch_to.window(driver.window_handles[2])
  
# get the new url in window 3
driver.get("https://www.geeksforgeeks.org/")
  
# print the 3rd window title
print(driver.title)

输出:

GeeksforGeeks | A computer science portal for geeks

解释:



该程序允许使用 get() 方法打开一个新窗口,该方法的参数为 geeks for geeks URL 并打印该窗口的标题。

示例 2:

蟒蛇3

# import modules
from selenium import webdriver
import time
  
# assign path and driver
PATH = "C:/chromedriver.exe"
driver = webdriver.Chrome(PATH)
  
# assign URL
driver.get("https://login.yahoo.com/")
print("First window title = " + driver.title)
  
# switch to new window
driver.find_element_by_class_name("privacy").click()
print(driver.window_handles)
driver.switch_to.window(driver.window_handles[1])
print("Second window title = " + driver.title)
  
# switch to new window
driver.execute_script("window.open()")
print(driver.window_handles)
driver.switch_to.window(driver.window_handles[2])
driver.get("https://www.geeksforgeeks.org/")
print(driver.title)

输出:

解释:

该脚本执行以下操作:

  1. 打开雅虎并打印标题。
  2. 从雅虎打开隐私并打印标题。
  3. 打开新窗口并在其上打开 geeks for geeks 并打印标题。