WebDriver Mouse Hover - Codoid
Select Page
Codoid Blog

WebDriver Mouse Hover

Irrespective of the business that your client is in, marketing is an integral part of their operations. In fact, in this day and age of social.

This is our very first article in Selenium, we hope it is helpful for Selenium users. Keep visiting Codoid blogs for more articles in the near future.

Using Custom JavaScript Mouse-event

//The below JavaScript code creates, initializes and dispatches mouse event to an object on fly.
String strJavaScript = "var element = arguments[0];"
            + "var mouseEventObj = document.createEvent('MouseEvents');"
            + "mouseEventObj.initEvent( 'mouseover', true, true );"
            + "element.dispatchEvent(mouseEventObj);";
	
//Then JavascriptExecutor class is used to execute the script to trigger the dispatched event.
((JavascriptExecutor) driver).executeScript(strJavaScript, element);
  

 

Using Device

//Getting mouse from driver object
Mouse mouse =((HasInputDevices)driver).getMouse();
	
//Invokes mouseMove method by passing element coordinates as argument
mouse.mouseMove(((Locatable)element).getCoordinates());
  

 

Using Advanced User Interactions API

Actions builder = new Actions(driver);

builder.moveToElement(element).perform();
//Note:  (Preferable for Firefox and HTMLUnit drivers)
  

Written By

Submit a Comment

Your email address will not be published. Required fields are marked *


This is our very first article in Selenium, we hope it is helpful for Selenium users. Keep visiting Codoid blogs for more articles in the near future.

Using Custom JavaScript Mouse-event

//The below JavaScript code creates, initializes and dispatches mouse event to an object on fly.
String strJavaScript = "var element = arguments[0];"
            + "var mouseEventObj = document.createEvent('MouseEvents');"
            + "mouseEventObj.initEvent( 'mouseover', true, true );"
            + "element.dispatchEvent(mouseEventObj);";
	
//Then JavascriptExecutor class is used to execute the script to trigger the dispatched event.
((JavascriptExecutor) driver).executeScript(strJavaScript, element);
  

 

Using Device

//Getting mouse from driver object
Mouse mouse =((HasInputDevices)driver).getMouse();
	
//Invokes mouseMove method by passing element coordinates as argument
mouse.mouseMove(((Locatable)element).getCoordinates());
  

 

Using Advanced User Interactions API

Actions builder = new Actions(driver);

builder.moveToElement(element).perform();
//Note:  (Preferable for Firefox and HTMLUnit drivers)