Selenium 4 window handling

Selenium 4 window handling

Window handling is an important technique in Selenium automated testing. With the release of Selenium 4, window handling has been greatly improved and enhanced. This article will explain how to use Selenium 4 to handle window manipulation.

Get the current window handle

To get the handle of the current window, you can use driver.current_window_handlethe method. The sample code is as follows:

current_handle = driver.current_window_handle

example

from selenium import webdriver

# 创建 ChromeDriver 实例
driver = webdriver.Chrome()

# 打开 CSDN 网站
driver.get("https://www.csdn.net/")

# 获取当前窗口句柄
current_handle = driver.current_window_handle

# 输出当前窗口句柄
print("当前窗口句柄:", current_handle)

# 在这里可以执行其他操作,例如点击链接、填写表单等

# 关闭浏览器窗口
driver.close()

# 退出 ChromeDriver
driver.quit()

output result

当前窗口句柄: 12AA3B8EAE3E20CAF9A97C1662A9AC9F

注解

The browser's current window handle (handle) is a string composed of numbers and letters, which is used to uniquely identify a window instance in the operating system. The reason for this identification lies in the underlying memory management and operating system design of the computer.

  1. Unique identification of memory address: The computer operating system allocates a memory address space for each process and application program to store its data and execute code. As part of the application, the window will also be allocated a memory space. These memory addresses are represented internally by the computer as binary numbers, but are not as readable and easy to use as hexadecimal (a combination of letters and numbers).
  2. Hexadecimal readability: Representing memory addresses as hexadecimal strings (consisting of numbers and letters) is easier for humans to understand and process. Compared with purely numeric memory addresses, hexadecimal is more compact and easy to distinguish, and can be conveniently used in logging and debugging information.
  3. Operating system cross-platform: Using a combination of numbers and letters as a window handle can ensure that the same identification method can be used under different operating systems and hardware architectures. This cross-platform is very valuable for developers and automated testing tools (such as Selenium), because they can maintain consistent code in different environments.

In short, the number and letter combination of the browser's current window handle is an identification method used to uniquely identify the window instance in the computer system, taking into account the needs of internal representation and the requirements of human readability.

Get all window handles

To get the handles of all open windows, you can use driver.window_handlesthe method. The sample code is as follows:

all_handles = driver.window_handles

example

from selenium import webdriver

# 创建 ChromeDriver 实例
driver = webdriver.Chrome()

# 打开第一个窗口并访问 CSDN 网站
driver.get("https://www.csdn.net/")

# 在第一个窗口打开新的标签页
driver.execute_script("window.open('', '_blank');")

# 获取所有窗口句柄
all_handles = driver.window_handles

# 遍历所有窗口句柄并输出
for handle in all_handles:
    print("窗口句柄:", handle)

# 在第二个标签页中打开 CSDN 博客首页
driver.switch_to.window(all_handles[1])
driver.get("https://blog.csdn.net/")

# 在这里可以执行其他操作,例如在不同标签页中进行切换、填写表单等

# 关闭浏览器窗口
driver.close()

# 退出 ChromeDriver
driver.quit()

Switch to the specified window

To switch to a specified window, driver.switch_to.window(handle)the method can be used, where handleis the window handle to switch to. The sample code is as follows:

target_handle = all_handles[1]  # 假设要切换到第二个窗口
driver.switch_to.window(target_handle)

example

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# 创建 ChromeDriver 实例
driver = webdriver.Chrome()

# 打开第一个网站
driver.get("https://www.baidu.com/")

# 等待第一个网站加载完成
wait = WebDriverWait(driver, 20)
wait.until(EC.title_contains("百度"))

# 在第一个窗口打开新的标签页(第二个网站)
driver.execute_script("window.open('https://juejin.cn/', '_blank');")

# 在第一个窗口打开新的标签页(第三个网站)
driver.execute_script("window.open('https://www.csdn.net/', '_blank');")

# 停顿10秒
time.sleep(10)

# 获取所有窗口句柄
all_handles = driver.window_handles

# 切换到第三个窗口
driver.switch_to.window(all_handles[2])

wait.until(EC.title_contains("CSDN"))  # 等待第三个网站加载完成

# 切换回第二个窗口
driver.switch_to.window(all_handles[1])
wait.until(EC.title_contains("掘金"))  # 等待第二个网站加载完成

# 打印第二个窗口的句柄和网页标题
second_window_handle = all_handles[1]
second_window_title = driver.title
print("第二个窗口的句柄:", second_window_handle)
print("第二个窗口的网页标题:", second_window_title)



# 关闭浏览器窗口
driver.quit()

code logic flow

  1. Import the required Selenium modules and classes.

  2. Create a ChromeDriver instance.

  3. Open the first website (Baidu) and wait for it to load.

  4. Open a new tab in the first window (the second website - Nuggets).

  5. Open a new tab in the first window (the third website - CSDN).

  6. Pause for 10 seconds to ensure that newly opened tabs have enough time to load their content.

    解释一下为什么这里要强制等待10秒:因为已经打开了三个网站,但是在测试时可能会受到网速影响,网页加载不完全,因为状态还是在等待加载中,没有完全加载完毕还是会继续执行后面的代码,
    从而报错发生异常
    所以还是要加上强制等待,利于观察到第三个完整加载完毕然后切换到第二个窗口的整个过程
    
  7. Get all window handles.

  8. Switch to the third window (CSDN website) and wait for the loading to complete.

  9. Switch back to the second window (Nuggets website) and wait for it to load.

  10. Print the second window's handle and page title.

  11. Close the browser window.

In general, the goal of this code is to open the three websites of Baidu, Nuggets and CSDN in one browser window, switch windows and wait for the page to load, and then print out the handle of the second window and the title of the page. The code uses WebDriver's wait mechanism (WebDriverWait) to ensure that the page is fully loaded when necessary. Finally, the browser window will be closed.

close current window

To close the current window, you can use driver.close()method. The sample code is as follows:

driver.close()

example

from selenium import webdriver

# 初始化浏览器驱动
driver = webdriver.Chrome()
import time
# 打开百度网站
driver.get("https://www.baidu.com/")

# 在新窗口中打开 CSDN 网站
driver.execute_script("window.open('https://www.csdn.net/', '_blank')")

# 获取所有窗口句柄
window_handles = driver.window_handles

# 切换到新窗口(CSDN 窗口)
driver.switch_to.window(window_handles[1])
time.sleep(5)
# 关闭 CSDN 窗口
driver.close()

# 切换回原始窗口(百度窗口)
driver.switch_to.window(window_handles[0])
# 获取并打印当前网页标题
print("当前网页标题:", driver.title)
time.sleep(5)

# 关闭浏览器
driver.quit()

Open the link in a new window

To open a link in a new window, you can use driver.execute_scriptthe method with JavaScript code to achieve. The sample code is as follows:

# 点击一个链接,新窗口会打开
link_element = driver.find_element(By.XPATH, "//a[contains(text(),'Open Link')]")
driver.execute_script("window.open(arguments[0], '_blank')", link_element.get_attribute("href"))

# 切换到新窗口
new_handle = driver.window_handles[-1]
driver.switch_to.window(new_handle)

example

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

# 初始化浏览器驱动
driver = webdriver.Chrome()

# 打开百度网站
driver.get("https://www.baidu.com")

# 点击链接,新窗口会打开
link_element = driver.find_element(By.LINK_TEXT, '新闻')
driver.execute_script("window.open(arguments[0], '_blank')", link_element.get_attribute("href"))

# 切换到新窗口
new_handle = driver.window_handles[-1]
driver.switch_to.window(new_handle)
sleep(5)
# 在新窗口进行操作
# ...

# 关闭新窗口
driver.close()

# 切换回原始窗口
original_handle = driver.window_handles[0]
driver.switch_to.window(original_handle)

# 获取并打印当前网页标题
print("当前网页标题:", driver.title)

sleep(5)

# 关闭浏览器
driver.quit()

These are the usual ways of dealing with windows in Selenium 4. Through these methods, windows can be easily controlled and manipulated to realize various scenarios in automated testing.

Guess you like

Origin blog.csdn.net/m0_67268191/article/details/132190490