问题描述
我正在使用 Selenium WebDriver 来自动化我的公司网站.在某一时刻,Web 应用程序通过以下方式打开一个新窗口:
I am working with Selenium WebDriver to automate my companies site. At one point, the web application opens a new window via the following:
<a onclick="WindowController.openWindow('quote.action?quoteProcessName=Shortform',WindowController.WINDOW_IS_TRACKED);" tabindex="1">Express Quote</a>
我们正在使用 jQuery,尽管我认为这是自定义的.我在测试团队,不做任何网站的开发.无论如何,它使用 JavaScript 打开一个新窗口.脚本单击此链接后,我需要将其附加到新窗口.
we are using jQuery, though I think this is custom. I am on the test team, and don't do any of the development of the site. Anyway, it uses JavaScript to open a new window. After the script clicks this link, I need it to attach to the new window.
问题是 WebDriver 在 IE9 中运行时似乎没有找到新窗口.这是我用来尝试切换到新窗口的代码:
The problem is that WebDriver doesn't seem to find the new window when running in IE9. Here's the code I am using to try and switch to the new window:
public boolean switchTo(final WebRobot robot, final String pageTitle) {
boolean found = false;
int count = 0;
while (!found && count < 20) {
final Set<String> handles = robot.getDriver().getWindowHandles();
final Iterator<String> itr = handles.iterator();
while (itr.hasNext()) {
try {
final String current = itr.next();
robot.getDriver().switchTo().window(current);
if (robot.getDriver().getTitle().contains(pageTitle)) {
robot.getLogger().debug("Switching to " + pageTitle);
found = true;
}
} catch (final NoSuchWindowException e) {
count++;
try {
Thread.sleep(2000);
System.out.println("Handles: " + robot.getDriver().getWindowHandles().size());
} catch (final InterruptedException ignored) {
//Nothing to do here
}
}
}
}
return found;
}
(WebRobot 是我编写的一个类,它允许我使用 WebDriver 轻松切换浏览器和执行模式.robot.getDriver() 返回 Selenium WebDriver 对象.)
(WebRobot is a class that I wrote to allow me to easily switch browsers and execution modes with WebDriver. robot.getDriver() returns the Selenium WebDriver object.)
新窗口打开后,robot.getDriver().getWindowHandles().size() 始终为1.是不是我缺少什么东西才能拿起新窗口?
After the new window opens, robot.getDriver().getWindowHandles().size() is always 1. Is there something I am missing to pick up the new window?
此代码在 Firefox、Chrome 和 IE8 中完美运行,但在 IE9 中无法正常运行.我正在使用 IEDriverServer 的 64 位版本,版本 2.32.3.0.我正在使用 Selenium WebDriver 2.32,并在 Windows 7 64bit 上运行.
This code works perfectly in Firefox, Chrome, and IE8, but not IE9. I am using the 64 bit edition of IEDriverServer, version 2.32.3.0. I am using Selenium WebDriver 2.32, and am running on Windows 7 64bit.
推荐答案
打开兼容模式解决了这个问题.
Turning on Compatibility Mode fixed the issue.
这篇关于Selenium IEServerDriver 找不到 IE9 的新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!