How to Perform Mouseover Function in Selenium Webdriver Using Java

How to perform mouseover function in Selenium WebDriver using Java?

Its not really possible to perform a 'mouse hover' action, instead you need to chain all of the actions that you want to achieve in one go. So move to the element that reveals the others, then during the same chain, move to the now revealed element and click on it.

When using Action Chains you have to remember to 'do it like a user would'.

Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath("html/body/div[13]/ul/li[4]/a"));
action.moveToElement(we).moveToElement(webdriver.findElement(By.xpath("/expression-here"))).click().build().perform();

How to mouse hover using java through selenium-webdriver and Java

To Mouse Hover over Electornics menu and select Camera & Photo you can use the following code block :

driver.get("http://demo.nopcommerce.com/");
Actions act = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement electronics = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li/a[@href='/electronics']")));
act.moveToElement(electronics).perform();
WebElement camera_n_photo = driver.findElement(By.xpath("//li/a[@href='/electronics']//following::ul/li/a"));
camera_n_photo.click();
System.out.println("Camera & photo Clicked.");

Mouseover function with Selenium Webdriver does not work with Opera 39 and works inconsistently with Chrome 53

In case anyone finds this in the future and is confused about why the mouse-over function works inconsistently with Chrome, Opera, or Internet Explorer, here is why:

The code I have above is correct. The problem is that for whatever reason, mouse over with Chrome, Opera, and IE doesn't work if the mouse cursor is on the browser window while the test is running (this may be an issue within the driver for each of these browsers).

To get around this, you need to ensure the mouse cursor is outside of the browser window while you run tests. I did this by leaving a pixel or two of space on the bottom of the screen when maximizing the browser window, and then using the java.awt.Robot class to move the mouse cursor to the bottom of the screen where the mouse would not interfere with the tests.

Below is an example for my monitor (which is 1680 x 1050, so I leave 40 pixels of space at the bottom of the screen):

    driver.manage().window().setPosition(new Point(0, 0));
org.openqa.selenium.Dimension d = new org.openqa.selenium.Dimension(1680, 1010);
driver.manage().window().setSize(d);

To move the cursor out of the way:

    Robot robot = new Robot();
robot.mouseMove(0, 1050);

You can call the above whenever you need to reset the mouse cursor to the bottom, for whatever reason.

How to hover over the button in selenium?

Go with Action class.

Actions hover = new Actions(driver);

WebElement Elem_to_hover = driver.findElementBy(By.id("id"));

hover.moveToElement(Elem_to_hover);

hover.build();

hover.perform();

How to do mouse hover using Selenium WebDriver in Firefox 19?

With the actions object you should first move the menu title, and then move to the popup menu item and click it. Don't forget to call actions.perform() at the end. Here's some sample Java code:

Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.linkText("Menu heading"));
actions.moveToElement(menuHoverLink);

WebElement subLink = driver.findElement(By.cssSelector("#headerMenu .subLink"));
actions.moveToElement(subLink);
actions.click();
actions.perform();

How to hover over an item avoiding a click on it using Selenium and C#

To hover over a certain button but to avoid clicking it you have to induce WebDriverWait for the desired ElementIsVisible() and the use Actions Class methods as follows:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
var element = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//a[normalize-space()='Wyloguj']")));

Actions action = new Actions(driver);
action.MoveToElement(element).Perform();


References

You can find a couple of relevant detailed discussions in:

  • Scrape website with dynamic mouseover event
  • How to mouse hover using java through selenium-webdriver and Java

Unable to perform Mouseover and click in selenium webdriver

Try with this, just another form of what vignesh posted below

new Actions(driver).moveToElement(menuHoverLink).perform();
new Actions(driver).moveToElement(subLink).perform();
subLink.click();


Related Topics



Leave a reply



Submit