IE9上的Selenium JavascriptExecutor导致';元素未滚动到视口中';错误

IE9上的Selenium JavascriptExecutor导致';元素未滚动到视口中';错误,javascript,selenium,internet-explorer-9,viewport,Javascript,Selenium,Internet Explorer 9,Viewport,我正在尝试使用JavascriptExecutor从selenium webdriver在IE9中打开一个新选项卡: public void openTab() { String url = webDriver.getCurrentUrl(); String script = "var a=document.createElement('a');a.target='_blank';a.href='" + url + "';a.innerHTML='open';docume

我正在尝试使用JavascriptExecutor从selenium webdriver在IE9中打开一个新选项卡:

public void openTab() {
    String url = webDriver.getCurrentUrl();
    String script = "var     a=document.createElement('a');a.target='_blank';a.href='" + url + "';a.innerHTML='open';document.body.appendChild(a);return a";
    Object element = getJSExecutor().executeScript(script);
    if (element instanceof WebElement) {
        WebElement anchor = (WebElement) element;
        anchor.click();
    } else {
        throw new RuntimeException("Unable to open tab: " + url);
    }
}
这在Chrome中运行良好,但在IE9中运行时,我遇到以下错误:

ElementNotVisibleException:驱动程序试图单击元素的点未滚动到视口中


我正在使用selenium和IEDriverServer的2.31版本。

设法解决了视口问题,并在一点劝诱后使IEDriverServer与两个窗口正确交互,因此我想我会发布我的解决方案,以防其他人有此问题

为了解决视口问题,我使用操作执行moveToElement,然后单击:

public void actionsClick(WebElement element){
    Actions builder = new Actions(webDriver);
    builder.moveToElement(element).click(element).perform();
}
IEDriverServer似乎需要更长的时间来获取所有窗口句柄,因此我在执行单击后在openTab方法中添加了5秒钟的等待:

public void openTab() {
    String url = webDriver.getCurrentUrl();
    String script = "var a=document.createElement('a');a.target='_blank';a.href='" + url + "';a.innerHTML='open me in a new tab';document.body.appendChild(a);return a";
    Object element = getJSExecutor().executeScript(script);
    if (element instanceof WebElement) {
        WebElement anchor = (WebElement) element;
        actionsClick(anchor);
        waitFor(5000);
        switchBrowserTab();
        returnToPreviousBrowserTab();
    } else {
        throw new RuntimeException("Unable to open tab: " + url);
    }
}
然后,如上面的方法所示,为了确保IEDriverServer知道这两个窗口/选项卡并可以在它们之间移动,我在单击并等待后添加了switchBrowserTab()和returnToPreviousBrowserTab()方法。 使用JavascriptExecutor打开一个新选项卡将焦点保留在原始选项卡中,并且该方法设置为再次以焦点返回该选项卡结束。 如果有人以前没有使用过窗口句柄,我将使用以下方法切换到新打开的选项卡:

    Set<String> handles = webDriver.getWindowHandles();
    List<String> handlesList = new ArrayList<String>();
    for (String handle : handles) {
        handlesList.add(handle);
    }
    webDriver.switchTo().window(handlesList.get(handlesList.size() - 1));
    webDriver.manage().window().maximize();
Set handles=webDriver.getWindowHandles();
List handlesList=new ArrayList();
for(字符串句柄:句柄){
handlesList.add(句柄);
}
webDriver.switchTo().window(handlesList.get(handlesList.size()-1));
webDriver.manage().window().maximize();
一种类似的方法用于向后移动,除了我获取当前句柄,然后在列表中循环找到它的位置,然后移动到从那里开始为-1的句柄

希望这是有帮助的


编辑:这适用于IE9和Chrome。没有在其他浏览器中测试。

我在parallels虚拟机的IE9中遇到了这个困难。这两条线让整件事顺利进行

Actions builder = new Actions(webDriver);
builder.moveToElement(element).click(element).perform();

将元素滚动到视图中,然后单击它。

IE可能没有时间渲染要添加到DOM中的元素,因此它的坐标不在浏览器视口中。您可以尝试在调用
click()
之前稍等片刻,看看是否有效。然而,IE驱动程序可能不会识别新标签。即使它能够识别,由于焦点问题,尝试单击新选项卡中的任何元素也可能无法工作。我担心您可能试图实现的变通方法,这是完全错误的方法。我正在尝试实现该变通方法,因为我必须使用选项卡进行测试,这是使用webdriver实现此目的的唯一方法。正如我所说的,这种方法在Chrome中运行得非常好,webdriver getWindowHandles()和switchTo()方法允许在选项卡/窗口之间切换。通常,我会放手,但“不得不使用选项卡”真的让我感到不舒服。我无法想象在新选项卡中打开页面与在新窗口中打开页面不相等的任何要求。如果用户已将浏览器设置设置为强制页面在新窗口中打开,该怎么办?我必须在新选项卡或窗口中打开页面-无论是哪个窗口。无论如何,我需要一种方法来实现这一点,我可以轻松地在它们之间切换,并且使用JavascriptExecutor允许我这样做。