IE9上的Selenium JavascriptExecutor导致'元素未滚动到视口'错误 [英] Selenium JavascriptExecutor on IE9 results in 'element was not scrolled into the viewport' error

查看:212
本文介绍了IE9上的Selenium JavascriptExecutor导致'元素未滚动到视口'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavascriptExecutor从selenium webdriver在IE9中打开一个新标签:

I'm trying to use a JavascriptExecutor to open a new tab in IE9 from selenium webdriver:

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中运行时,我收到以下错误:

This works fine in Chrome but on running in IE9, I get the following error:

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

ElementNotVisibleException: The point at which the driver is attempting to click on the element was not scrolled into the viewport.

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

I'm using the 2.31 versions of both selenium and IEDriverServer.

推荐答案

管理来解决视口问题&在稍微哄骗之后让IEDriverServer与两个窗口正常交互,以为我会发布我的解决方案以防其他人遇到此问题。

Managed to resolve the viewport issue & get the IEDriverServer to interact properly with both windows after a little coaxing so thought I'd post my solution in case anyone else has this problem.

要解决视口问题,我使用Actions来执行moveToElement然后单击:

To resolve the viewport problem, I used Actions to do a moveToElement then click:

public void actionsClick(WebElement element){
    Actions builder = new Actions(webDriver);
    builder.moveToElement(element).click(element).perform();
}

IEDriverServer似乎需要更长时间才能拿起所有窗口句柄,所以我在点击后向openTab方法添加了5秒等待:

The IEDriverServer seems to take a little longer to pick up all the window handles, so I added a 5 second wait into the openTab method after doing the click:

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打开一个新选项卡会将焦点保留在原始选项卡中,并且此方法设置为以焦点重新结束。
如果以前有人没有使用过窗口句柄,这里是我用来切换到新打开的标签的方法:

Then, as shown in the method above, to ensure the IEDriverServer is aware of both windows/tabs and can move between them, I added switchBrowserTab() and returnToPreviousBrowserTab() methods after clicking and waiting. Using the JavascriptExecutor to open a new tab will leave focus in the original tab and this method is set up to end with focus back in there again. In case anyone hasn't used the window handles before, here is the method I'm using for switching to the newly opened tab:

    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();

类似的方法用于向后移动,除了我得到当前句柄,然后循环遍历列表找到它的位置,然后从那里移动到-1的句柄。

A similar approach is used to move back except I get the current handle, then loop through the list to find its position, then move to the handle that is -1 from there.

希望这有用。

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

this works in IE9 and Chrome. Not tested in other browsers.

这篇关于IE9上的Selenium JavascriptExecutor导致'元素未滚动到视口'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆