Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Hover over and click on an invisible element using selenium webdriver?

There is an invisible element on my HTML page which becomes visible when a mouse hover is done on the element. What I Have to do is

  1. Hover over the element
  2. Click on the element (it will display 4 options)
  3. Click on one of the options

I am using Java API for selenium web driver and following is what I have been trying

Actions builder = new Actions(driver);
builder.moveToElement(MainMenuBTN).click().build().perform();

subMenuBTN.click();
  1. MainMenuBTN = element that becomes visible when you hover the mouse over it
  2. subMenuBTN = element that is being chosen out of the menu options that are displayed

What is happening is, the click() on MainMenuBTN is generating ElementNotVisible exception. I tried following to avoid this, but did not work.

Actions builder = new Actions(driver);
builder.moveToElement(mainMenuBTN).build().perform();
builder.click();

subMenuBTN.click();

A Note : mainMenuBTN and subMenuBTN are WebElements generated by

driver.findElement(By.xpath("xpath_string"))

Am I missing anything? Help appreciated !

like image 390
MePunekar Avatar asked Oct 17 '12 00:10

MePunekar


People also ask

How do you select invisible element in Selenium?

We can click on an element which is hidden with Selenium webdriver. The hidden elements are the ones which are present in the DOM but not visible on the page. Mostly the hidden elements are defined by the CSS property style="display:none;".

Can Selenium click on hidden element?

Selenium by default cannot handle hidden elements and throws ElementNotVisibleException while working with them. Javascript Executor is used to handle hidden elements on the page. Selenium runs the Javascript commands with the executeScript method.

How do you hover an element in Selenium?

The first step here would be to locate the main menu (AKA parent menu). Once that is done, the second step is to locate the desired element (child element) from the available options in the sub-menu. The final step would be to click on that child element.

How do you click on a submenu that is only visible on mouse hover in a menu?

This method shall move the mouse to the middle of the menu which displays submenu on mouse over. Then apply the perform method to actually perform this action. After hovering on the menu, we shall select a sub-menu with the help of the click method.


2 Answers

Well, after going through your questions numerous times and changing my answers many times I will go with -

Issue - what I got from the original code -

You need to move the cursor to the mainMenuBTN (which is visible not the element that becomes visible when you hover the mouse over it ) and subMenuBTN is then displayed which you need to click.

The only edit to your original code as per me will be adding a statement to move the cursor to your subMenuBTN before you click it. This way works fine for me when I need to click sub menu item.

Actions builder = new Actions(driver);
builder.moveToElement(mainMenuBTN).build().perform();
builder.moveToElement(subMenuBTN).build().perform();
subMenuBTN.click();

Please let me know if this is the case.

like image 71
some_other_guy Avatar answered Oct 05 '22 22:10

some_other_guy


using javascript executor like

((JavascriptExecutor) webdriver).executeScript("document.getElementById('btn').click();");
like image 44
Amareswar Avatar answered Oct 05 '22 23:10

Amareswar