c# - 使用 Selenium 使用 WindowHandles 跟踪和遍历选项卡和窗口的最佳方法

标签 c# selenium selenium-webdriver webdriverwait window-handles

我们正在使用 Selenium webdriver 为 Internet Explorer 11 进行 UI 测试。 在经过测试的 Web 应用程序中,弹出了几个屏幕。在几个测试中,我们最终得到了三个浏览器窗口,因此也是三个 Driver.WindowHandles。 要从一个 WindowHandle 切换到另一个 WindowHandle,我们希望 Driver.WindowHandles 的排序方式是最旧的窗口在前,最新的窗口在后。但事实并非如此:它完全是随机的!

因为窗口句柄是一个 GUID,所以我们最终创建了一个字典,其中 WindowHandle GUID 作为键,其值是在浏览器窗口中加载的页面类型。 但这也会导致在例如关闭窗口时维护字典。

对于这么简单的事情,这似乎是很多工作。对此有更好的解决方案吗?

最佳答案

你说的很对:

WindowHandles would be sorted like the oldest windows first and the newest windows last. But this is not the case: It is totaly random!

在一次讨论中,Simon 明确提到:

While the datatype used for storing the list of handles may be ordered by insertion, the order in which the WebDriver implementation iterates over the window handles to insert them has no requirement to be stable. The ordering is arbitrary.

因此,我们将引入一个WebDriverWait,然后在每次打开新选项卡/窗口时收集窗口句柄,最后遍历窗口句柄和 switchTo().window(newly_opened) 根据需要:

Please adjust the Test Environment if needed [My configuration - Selenium: 3.5.3, IEDriverServer: 3.5.0.0 (64-bit), IE: v10.0]

Java:

package demo;

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 NEW_TAB_Handling {

    public static void main(String[] args)  {


        System.setProperty("webdriver.ie.driver", "C:\\Utility\\BrowserDrivers\\IEDriverServer.exe");
        WebDriver driver =  new InternetExplorerDriver();
        driver.get("http://www.google.com");
        String first_tab = driver.getWindowHandle();
        System.out.println("Working on Google");
        ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
        WebDriverWait wait = new WebDriverWait(driver,5);
        wait.until(ExpectedConditions.numberOfWindowsToBe(2));
        Set<String> s1 = driver.getWindowHandles();
        Iterator<String> i1 = s1.iterator();
        while(i1.hasNext())
        {
            String next_tab = i1.next();
            if (!first_tab.equalsIgnoreCase(next_tab))
            {
                driver.switchTo().window(next_tab);

                System.out.println("Working on Facebook");
            }
        }
        String second_tab = driver.getWindowHandle();
        ((JavascriptExecutor) driver).executeScript("window.open('http://youtube.com/');");
        wait.until(ExpectedConditions.numberOfWindowsToBe(3));
        Set<String> s2 = driver.getWindowHandles();
        Iterator<String> i2 = s2.iterator();
        while(i2.hasNext())
        {
            String next_tab = i2.next();
            if (!first_tab.equalsIgnoreCase(next_tab) && !second_tab.equalsIgnoreCase(next_tab))
            {
                driver.switchTo().window(next_tab);
                System.out.println("Working on Youtube");
            }
        }
        driver.quit();
        System.out.println("Quit the WebDriver instance");
    }
}

控制台输出:

Working on Google
Working on Facebook
Working on Youtube
Quit the WebDriver instance

结尾

您可以找到 基于 Open web in new tab Selenium + Python 中的讨论

关于c# - 使用 Selenium 使用 WindowHandles 跟踪和遍历选项卡和窗口的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46251494/

相关文章:

java - 如何获取 WebElement 的父级

selenium - 在 selenium webdriver 中的浏览器上测试页面的缩放级别

node.js - 如何通过 NodeJS Selenium 驱动程序使用代理?

c# - 如何使用 C# 从网页打开事件查看器?

c# - 实现地理坐标类 : equality comparison

c# - 从即将重定向到的页面后面的代码调用方法/函数?

selenium - 如何在不重复的情况下同时使用 'When' 和 'And' 来实现相同的功能

node.js - 用于 NodeJS 的 Selenium Webdriver 的文档在哪里?

java - 在 Selenium webdriver 中跳过 SVG 条形图上的元素

c# - 从 utr 执行 Google/Bing map 搜索