Mouse Hover Actions in Selenium Webdriver

In order to perform a 'mouse hover' action, we need to chain all of the actions that we want to achieve in one go. So move to the element that which has sub elements and click on the child item. It should the same way what we do normally to click on a sub menu item.

With the actions object you should first move the menu title, and then move to the sub menu item and click it.

First we need to create new action builder instance by passing the webdriver instance, then.

Below is the sample code to perform Mouse hover action

Example 1:

Actions actions = new Actions(driver);
WebElement mainMenu = driver.findElement(By.linkText("menulink"));
actions.moveToElement(mainMenu);

WebElement subMenu = driver.findElement(By.cssSelector("subLinklocator"));
actions.moveToElement(subMenu);
actions.click().build().perform();

Here 'build()' method is used to compile all the list of actions into a single step and ready to be performed.
Example 2:

Actions action = new Actions(driver);
WebElement mainMenu = driver.findElement(By.linkText("MainMenu"));
action.moveToElement(mainMenu).moveToElement(driver.findElement(By.xpath("submenuxpath"))).click().build().perform();

There are cases where you may just want to mouse hover on particular element and check if the button state/color is changing after mouse hover.

Below is the example to perform mouse hover

WebElement searchBtn = driver.findElement(By.id("searchbtn"));

Actions action = new Actions(driver);
action.moveToElement(searchBtn).perform();

Click here for more mouse hover methods, keyboard events and examples

Selenium Tutorials: 

Comments

Hi,
I have tried using sendKeys to upload the file but it is not working.

Please help me to upload the file. This is my webpage link http://www.burbank.com.au/victoria/careerdetail/draftsperson-urban-commu...
in this I want to upload cover letter and Resume.

I have done with driver.findElement(By.id("uploadcoverimg")).sendKeys("C:\\Users\\hadmin\\Desktop\\test.doc");
driver.findElement(By.xpath(".//*[@id='uploadResumeTrigger']/img")).sendKeys("C:\\Users\\hadmin\\Desktop\\test.doc");

But it is not working.
Please help ASAP

This comment suppose to be in Upload using sendKeys

And it has been made clear that, input tag should be in enable mode.
If you observe the above one, the input tag is in disable mode. You can do that by enabling it.

Hi Prem, you cannot handle window based applications in selenium webdriver,so I would request you to use 3rd party like Auto It tool which is used to upload files from windows.

use Auto It for upload and download in Selenium WebDriver

Haa yaar ye upar wala sahi bol ria h...

while i am doing mouse over action... iam getting this error "BeanShell 2.0b4 - by Pat Niemeyer (pat@pat.net)
bsh % "

can anyone know how to get the text when we mouseover on a particular web element which doesn't have any locator like id, name. etc.

You can construct xpath or css, we need locator to perform any operation.

Hi ,

I need a code To select a value from list when mouse hover on Image.

I need to test text,popup when mouse move into button.(eg: click me)

Add new comment