4

我正在使用 Selenium WebDriver 来自动化我的公司网站。在某一时刻,Web 应用程序通过以下方式打开一个新窗口:

<a onclick="WindowController.openWindow('quote.action?quoteProcessName=Shortform',WindowController.WINDOW_IS_TRACKED);" tabindex="1">Express Quote</a>

我们正在使用 jQuery,尽管我认为这是自定义的。我在测试团队,不做任何网站的开发。无论如何,它使用 JavaScript 打开一个新窗口。脚本单击此链接后,我需要将其附加到新窗口。

问题是 WebDriver 在 IE9 中运行时似乎没有找到新窗口。这是我用来尝试切换到新窗口的代码:

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 对象。)

新窗口打开后,robot.getDriver().getWindowHandles().size() 始终为 1。我是否缺少某些东西来拾取新窗口?

此代码在 Firefox、Chrome 和 IE8 中完美运行,但不适用于 IE9。我正在使用 IEDriverServer 的 64 位版本,版本 2.32.3.0。我正在使用 Selenium WebDriver 2.32,并且在 Windows 7 64 位上运行。

4

2 回答 2

0

http://grokbase.com/t/gg/webdriver/128hgbmcw0/getwindowhandles-return-only-1-window-in-ie

newSet.removeAll(oldSet); newWindow = newSet.toArray()[0];

于 2013-05-22T10:11:53.467 回答
0

打开兼容模式解决了这个问题。

于 2014-10-10T19:33:58.030 回答