乐闻世界logo
搜索文章和话题

What is the difference between driver.getWindowHandle and driver.getWindowHandles in Selenium?

1个答案

1
  1. driver.getWindowHandle():

    • This method retrieves the handle of the current browser window, which serves as a unique identifier. Each browser window has a distinct handle, and this method returns a string value representing the handle of the window currently controlled by Selenium.

    • For example, if you are working with a browser window and need to retrieve its handle, you can use this method.

    Example code:

    python
    current_window_handle = driver.getWindowHandle() print("Current window handle:", current_window_handle)
  2. driver.getWindowHandles():

    • Unlike getWindowHandle(), getWindowHandles() retrieves the handles of all open browser windows in the current session. This method returns a set of handles (typically a set of string values), containing unique identifiers for all windows.

    • This method is particularly useful when switching between multiple windows or performing operations across different windows.

    Example code:

    python
    all_window_handles = driver.getWindowHandles() for handle in all_window_handles: print("Available window handle:", handle) if handle != current_window_handle: driver.switchTo().window(handle) print("Switched to new window:", driver.title)

In practical applications, use getWindowHandle() when checking or operating on a specific open window. However, for test scenarios involving multiple windows—such as opening a new window from one and switching between them—getWindowHandles() is more applicable. These two methods are frequently used together in automation test scripts to effectively manage multiple browser windows.

2024年8月14日 00:01 回复

你的答案