Unable to get new window handle with Selenium WebDriver in Java on IE(无法在 IE 上使用 Java 中的 Selenium WebDriver 获取新窗口句柄)
问题描述
当我单击页面上的按钮(在 IE 浏览器中)时,会打开一个新的弹出页面.我尝试获取此弹出窗口的窗口句柄失败了.这是我的第一次尝试:
When I click a button on a page (in IE browser), a new popup page opens. My attempts to get the window handle for this popup have failed. Here is my first attempt:
String baseWin = driver.getWindowHandle();
System.out.println(baseWin);
Set<String>s = driver.getWindowHandles();
Iterator<String> ite = s.iterator();
while ( ite.hasNext() ) {
String popUpHandle = ite.next();
if(!baseWin.equals(popUpHandle)) {
driver.switchTo().window(popUpHandle);
System.out.println(driver.switchTo().window(popUpHandle).getTitle());
此尝试仅打印基本窗口的句柄,如果第二个打印语句放置在 if() 语句之外,但在 while() 语句中和 if() 语句之后,它只会输出基本窗口的标题.所以这组句柄似乎只包含基本窗口句柄.
This attempt prints only the base window's handle, and if the second print statement is placed outside the if() statement, but within the while() statement and after the if() statement, it simply outputs the base window's title. So the set of handles only seems to contain the base window handle.
这是我的第二次尝试:
String baseWin = driver.getWindowHandle();
System.out.println(baseWin);
ArrayList<String> popUpWin = new ArrayList<String>(driver.getWindowHandles());
popUpWin.remove(baseWin);
driver.switchTo().window(popUpWin.get(0));
System.out.println(driver.switchTo().window(popUpWin.get(0)));
此尝试返回错误,表示数组 popUpWin 为空,即 size == 0.因此,当我调用 driver.getWindowHandles() 时未检索弹出窗口的句柄,并且仅包含基本窗口的句柄.这是IE的问题吗?有解决方法吗?还是我忽略了代码中的某些内容?(请注意,我忽略了此处包含的代码中的暂停,因此我认为这不是问题所在.)
This attempt returns an error, which says that the array popUpWin is empty, i.e. size == 0. So, the popup window's handle isn't being retrieved when I call driver.getWindowHandles() and only contains the base window's handle. Is this an IE issue? Is there a workaround? Or am I overlooking something in my code? (Note that I've neglected pauses in the code that I've included here, so I don't believe that is the issue.)
推荐答案
你需要做两件事:
更改IE浏览器的安全设置:
Change the security setting in IE browser:
打开IE浏览器,点击Internet选项"=>安全"=>勾选Internet"、本地Intranet"、受信任站点"和受限站点"的启用保护模式".
Open IE browser, click on "Internet Options" => "Security" => tick "Enable Protected Mode" for "Internet", "Local intranet", "Trusted sites" and "Restricted sites".
这使 IE 驱动程序能够控制新窗口句柄,这样当您调用driver.getWindowHandles();
或 driver.getWindowHandles().size();
您将获得所有句柄,包括原始窗口和新窗口.更准确地说,您只需将所有 4 个域的安全设置设置为相同,这意味着您可以取消选中所有 4 个域的启用保护模式",但显然不鼓励这样做.
This gives IE driver the capability to get control of the new window handle, so that when you call
driver.getWindowHandles();
or driver.getWindowHandles().size();
you will get all the handles including the original window and the new windows. To be more accurate, you just need to set the security setting for all 4 domains to be the same which means you can uncheck "Enable Protected Mode" for all 4 domains but it is discouraged obviously.
调用driver.switchTo().window(windowName);
后,需要添加((JavascriptExecutor) driver).executeScript("window.focus();");
在 IE 驱动程序可以对窗口执行任何操作之前.
After you call driver.switchTo().window(windowName);
, you need to add ((JavascriptExecutor) driver).executeScript("window.focus();");
before the IE driver can perform any actions on the window.
这是因为 IE 驱动程序需要它正在处理的窗口处于前台,这条线帮助驱动程序获得窗口的焦点,以便它可以在窗口上执行您想要的任何操作.
This is because IE driver needs the window that it is working on to be at the foreground, this line helps the driver get the focus of the window so that it can perform any actions on window that you want.
以下为完整示例:
String baseWin = driver.getWindowHandle();
//Some methods to open new window, e.g.
driver.findElementBy("home-button").click();
//loop through all open windows to find out the new window
for(String winHandle : driver.getWindowHandles()){
if(!winHandle.equals(baseWin)){
driver.switchTo().window(winHandle);
//your actions with the new window, e.g.
String newURL = driver.getCurrentUrl();
}
}
//switch back to the main window after your actions with the new window
driver.close();
driver.switchTo().window(baseWin);
//let the driver focus on the base window again to continue your testing
((JavascriptExecutor) driver).executeScript("window.focus();");
这篇关于无法在 IE 上使用 Java 中的 Selenium WebDriver 获取新窗口句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法在 IE 上使用 Java 中的 Selenium WebDriver 获取新窗口句柄
基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01