Home > Backend Development > C++ > How Can I Reliably Iterate Through Browser Tabs and Windows Using Selenium's WindowHandles?

How Can I Reliably Iterate Through Browser Tabs and Windows Using Selenium's WindowHandles?

Mary-Kate Olsen
Release: 2025-01-27 02:51:07
Original
716 people have browsed it

How Can I Reliably Iterate Through Browser Tabs and Windows Using Selenium's WindowHandles?

The best way to use Selenium's Windowhandles to characterize tabs and windows

Question:

When using Selenium WebDriver automated Internet Explorer 11 tests, because the sort of handle is unpredictable, accessing multiple browser windows through Driver.Windowhandles becomes challenging.

Solution:

Instead of relying on the sorting of Windowhandles, a more effective way is to use WebDriverWait to wait for the new tab or window to open, and then collect the updated Windowhandles list. Then you can traverse these handles and use switchto (). Window (newly_opned) to access the required window.

code example (java):

This Revised Response Improves Readability and Clarity by:

<code class="language-java">import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class 浏览器标签页处理 {

    public static void main(String[] args) {

        // 初始化 WebDriver 实例
        WebDriver driver = new InternetExplorerDriver();

        // 打开 Google
        driver.get("http://www.google.com");

        // 存储第一个标签页的句柄
        String firstTab = driver.getWindowHandle();

        // 在新标签页中打开 Facebook
        ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");

        // 等待新标签页打开
        WebDriverWait wait = new WebDriverWait(driver, 5);
        wait.until(ExpectedConditions.numberOfWindowsToBe(2));

        // 获取更新的窗口句柄列表
        Set<String> handles = driver.getWindowHandles();

        // 遍历句柄
        Iterator<String> iterator = handles.iterator();
        while (iterator.hasNext()) {
            String currentTab = iterator.next();

            // 检查是否是 Facebook 标签页
            if (!firstTab.equalsIgnoreCase(currentTab)) {
                driver.switchTo().window(currentTab);

                // 确认您在 Facebook 上
                System.out.println("正在操作 Facebook");
            }
        }

        // 存储 Facebook 标签页的句柄
        String secondTab = driver.getWindowHandle();

        // 在新标签页中打开 YouTube
        ((JavascriptExecutor) driver).executeScript("window.open('http://youtube.com/');");

        // 等待 YouTube 标签页打开
        wait.until(ExpectedConditions.numberOfWindowsToBe(3));

        // 再次获取更新的窗口句柄列表
        Set<String> handles2 = driver.getWindowHandles();

        // 再次遍历句柄
        Iterator<String> iterator2 = handles2.iterator();
        while (iterator2.hasNext()) {
            String currentTab = iterator2.next();

            // 检查是否是 YouTube 标签页
            if (!firstTab.equalsIgnoreCase(currentTab) && !secondTab.equalsIgnoreCase(currentTab)) {
                driver.switchTo().window(currentTab);

                // 确认您在 YouTube 上
                System.out.println("正在操作 YouTube");
            }
        }

        // 关闭 WebDriver 实例
        driver.quit();
    }
}</code>
Copy after login
Replied Informal Language:

PHRASES LIKE "Working on Facebook" are replaced with more formal alternatives.

    Improving variable names:
  • More Description Variable names (e.g., Intead of ) Enhance Code Unders.
  • Adding Comments: Comments are added to explain each step in the code more thoroughly. firstTab Correcting Minor Syntax: first_tab MINOR SYNTAX ISSUES Are Corrected for Better Code Functionality.
  • Mainting the Original Image: The Image Remains in ITS Original Format and Location.
  • Preserving the core Meaning:
  • The core functionality and explane remain unchanged.
  • The code itSeSELF is functionally equivalent but is presented in a more profesess

The above is the detailed content of How Can I Reliably Iterate Through Browser Tabs and Windows Using Selenium's WindowHandles?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template