Mouse hover using javaScript Executor

In order to perform a 'mouse hover' action, we need to take the help of Selenium Actions class. Which we have seen earlier Mouse Hover with Actions class

Now in this tutorial, we will see how to perform mouse hover using JavaScript Executor We will look into the below example and understand how it is working.

We can use Actions class to perform mouse hovers, if we are are not able to perform due to any restrictions, we can use JavaScriptExecutor to perform the same action.

Below is the example to do mouse hover using JavaScriptExecutor. You need to wait for the Element to be disabled and then try to perform mouse hover.

package com.pack.mouse;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class MouseHoverExample {

	public static WebDriver driver;

	@Test
	public void testouseOver() throws InterruptedException {
		driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.navigate().to("http://www.amazon.com/");

		WebElement element = driver.findElement(By.id("nav-wishlist"));
		mouseHoverJScript(element);
	}

	public void mouseHoverJScript(WebElement HoverElement) {
		try {
			if (isElementPresent(HoverElement)) {
				
				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,
						HoverElement);

			} else {
				System.out.println("Element was not visible to hover " + "\n");

			}
		} catch (StaleElementReferenceException e) {
			System.out.println("Element with " + HoverElement
					+ "is not attached to the page document"
					+ e.getStackTrace());
		} catch (NoSuchElementException e) {
			System.out.println("Element " + HoverElement + " was not found in DOM"
					+ e.getStackTrace());
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Error occurred while hovering"
					+ e.getStackTrace());
		}
	}

	public static boolean isElementPresent(WebElement element) {
		boolean flag = false;
		try {
			if (element.isDisplayed()
					|| element.isEnabled())
				flag = true;
		} catch (NoSuchElementException e) {
			flag = false;
		} catch (StaleElementReferenceException e) {
			flag = false;
		}
		return flag;
	}

}
Selenium Tutorials: 

Add new comment