栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Selenium的WindowHandles跟踪选项卡和窗口并进行迭代的最佳方法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Selenium的WindowHandles跟踪选项卡和窗口并进行迭代的最佳方法

您说的很正确:

WindowHandles的排序方式将像最旧的窗口在前,最新的窗口在后。但是事实并非如此:这完全是随机的!

在讨论中,Simon明确提到:

While the datatype used for storing the list of handles may be ordered byinsertion, the order in which the WebDriver implementation iterates over thewindow handles to insert them has no requirement to be stable. The orderingis arbitrary.

因此,

WebDriverWait

每次打开新的标签页/窗口时,我们都会引入,然后收集窗口句柄,并最终
switchTo().window(newly_opened)
根据需要遍历窗口句柄:

请调整

Test Environment
如果需要的话[我的配置-
Selenium
:3.5.3
IEDriverServer

:3.5.0.0(64位)
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 GoogleWorking on FacebookWorking on YoutubeQuit the WebDriver instance

奥托罗

您可以在新标签Selenium + Python中的Open
web中找到基于python的讨论



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/442825.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号