1

I need to do mouseover function using JavaScript, I have tried the below code am not getting any error but mouseover function not happening.

      WebElement Mainmenu=_driver.findElement(By.xpath(locatorvalue[0]));
    String strJavaScript = "var element = arguments[0];"
            + "var mouseEventObj = document.createEvent('MouseEvents');"
            + "mouseEventObj.initEvent( 'mouseover', true, true );"
            + "element.dispatchEvent(mouseEventObj);";
     JavascriptExecutor js =  (JavascriptExecutor) _driver;
     js.executeScript(strJavaScript, Mainmenu);
9
  • You can use actions class to perform mouse related activities. Commented Oct 10, 2013 at 6:26
  • already i used action class it not working in all browsers and all application,Thats y am going through javascript Commented Oct 10, 2013 at 6:29
  • String javaScript = "var evObj = document.createEvent('MouseEvents');" + "evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" + "arguments[0].dispatchEvent(evObj);"; ((JavascriptExecutor) Driver.driver).executeScript(javaScript, element); This should work. Give it a try. Commented Oct 10, 2013 at 6:36
  • no boss its not working Commented Oct 10, 2013 at 6:49
  • Can you give use the web-page you're trying to work on so that we can better understand what "not working" means? Commented Oct 10, 2013 at 6:53

2 Answers 2

2
public void click(WebElement a,WebElement b)
{
    try 
     {
         String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover',true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
         ((JavascriptExecutor) driver).executeScript(mouseOverScript,a);
         Thread.sleep(1000);
         ((JavascriptExecutor) driver).executeScript(mouseOverScript,b);
         Thread.sleep(1000);
         ((JavascriptExecutor)driver).executeScript("arguments[0].click();",b);


    } catch (Exception e) {
        // TODO: handle exception
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
((JavascriptExecutor)driver).executeScript("$('element_selector').hover();");

Hope this will help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.