Actions Class not working with gecko driver

2020-02-09 08:34发布

问题:

I am facing a problem with Actions class driver. I have this piece of code

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

Previously when i am using Selenium-Java 2.43.0 , this command is working fine. But i have upgraded to 3.0.0-beta2, started access firefox driver through gecko driver.

At the above specified command my test is failing . Am getting the below exception

org.openqa.selenium.UnsupportedCommandException: POST /session/21dfc828-a382-4622-8c61-89bc48e29744/moveto did not match a known command (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 4 milliseconds

Please help

回答1:

Temporary, awful, depressing answer until they fix this is to revert back to the working versions of Selenium and Firefox. Selenium 2.53.0 with Firefox 45.0.2 is still working: https://ftp.mozilla.org/pub/firefox/releases/45.0.2/

I regret not testing against the latest, but at the same time it beats not having any Firefox tests run at all. Not running against Firefox for months on end is unacceptable.



回答2:

It's a versioning problem. Selenium 3 has no support yet for Actions class driver. You will have to download to a lower version. Version 2.53.1 works fine for me with Firefox



回答3:

The below works for me on Firefox 52.3.0 ESR and 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);
}