使用Gecko Driver时,Actions类无法正常工作

13

我在使用 Actions 类的驱动时遇到了问题。我有这段代码:

Actions act= new Actions(d1);
act.moveToElement(d1.findElement(By.xpath("path of the element")).build().perform();

之前我使用 Selenium-Java 2.43.0 时,这个命令可以正常工作。但是我升级到了 3.0.0-beta2,开始通过 gecko driver 访问 firefox driver

在上述命令中,我的测试失败了。我收到了以下异常:

org.openqa.selenium.UnsupportedCommandException: POST /session/21dfc828-a382-4622-8c61-89bc48e29744/moveto 未匹配到已知命令(警告:服务器未提供任何堆栈跟踪信息) 命令执行时间或超时:4毫秒

请帮忙解决问题。


这个问题已记录在Selenium的open issues中。我们需要等待修复。https://github.com/SeleniumHQ/selenium/issues/2285。那就是问题所在。也许我们需要等待一段时间。 - naveen kumar
截至2017年3月18日,该问题似乎已经得到解决:“这现在已经在geckodriver 0.15和Selenium 3.x中实现”,并且该问题已关闭。但是我仍然遇到了一些非常大的鼠标悬停问题 :/ - George Pantazes
3个回答

5

这是一个版本问题。Selenium 3目前还不支持Actions类驱动程序。你需要下载低版本的Selenium。版本2.53.1在我使用Firefox时完美运行。


1
有关这个的任何消息吗?这个计划在某个时候实施吗,希望很快? - nostradamus

5
暂时的、糟糕的、令人沮丧的解决方案是回退到Selenium和Firefox的工作版本。Selenium 2.53.0 和 Firefox 45.0.2 仍在运行中: https://ftp.mozilla.org/pub/firefox/releases/45.0.2/ 很遗憾我没有测试最新版本,但与完全没有Firefox测试相比,它仍然是一个不错的选择。长时间不运行Firefox测试是不可接受的。

0

以下内容适用于Firefox 52.3.0 ESR和Selenium 3.5.1

public void mouseRightClickAndSelectOption(By locator, By contextMenuOption){
    clickElement(locator);
    String script = "var evt = document.createEvent('MouseEvents');" + "evt.initMouseEvent('contextmenu',true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0,null);" + "arguments[0].dispatchEvent(evt);";

    try {
        ((JavascriptExecutor) driver).executeScript(script, getElement(locator));
    } catch (Exception ignored) {
    }
    clickElement(contextMenuOption);
}


public WebElement getElement(By locator) {
    fluentWait(locator);
    return driver.findElement(locator);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接