Selenium WebDriver : Handling mouseover events

I was searching though the internet to find out proper way of handling mouseover events with Selenium WebDriver. Found few resources but nothing proven to work. However finally managed to get though it. For others convenient, I though of writing down the way we handle mouse over events in our automation tests.


In order to get the job done, you need to use org.openqa.selenium.interactions.Actions class. The Action class is user-facing API for emulating complex user gestures. WebDirver users can use this class to simulate usage of keyboard or mouse events. It Implements builder pattern. So calling build will build   composite action containing all actions specified by the method calls.

I have used following code snippets to delete a tag in the tag cloud. The delete button of a particular tag gets visible only for mouse hover event.

Actions builder = new Actions(driver);
WebElement tagElement = driver.findElement(By.id("tag-cloud"));
builder.moveToElement(tagElement).build().perform();

You need to create new action builder instance by passing WebDriver instance to the class. Then move to the WebElement and build it to generates a composite action containing all actions so far, ready to be performed.

This will be the first post of Selenium WebDriver tips. Some useful aspects and solutions for common UI automation requirements using WebDriver will be published as i came across them.