13

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 !

1
  • Did u find any solution to this? For me following code works : WebElement menu = driver.findElement(by); Actions builder = new Actions(driver); builder.moveToElement(menu).build().perform(); WebDriverWait wait = new WebDriverWait(driver, 15); After the sub menus are displayed i find the element using id and click on it. Unfortunately this works fine with FF 25 and Selenium 2.42.2. When i upgrade FF it doesn't work as expected. Commented Jul 14, 2015 at 12:40

6 Answers 6

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply. The above code gives ElementNotVisible exception at "subMenuBTN.click();". Also, I need to hover and click the mainMenuBTN to get the list from which i can click the specific subMenuBTN. I am not sure if build().perform() will actually click on the mainMenuBTN.
build().perform() won't click the mainMenuBTN. But why do you need to hover on the mainMenuBTN??? Can't you click it directly?
Unfortunately, I can not click the main menu button. The hover over event activates the button, then I need to click that button to open a sub menu.
@Some_other_guy - even i faced this issue. Your solution worked !!
7

using javascript executor like

((JavascriptExecutor) webdriver).executeScript("document.getElementById('btn').click();");

6 Comments

Thanks for the reply! Where to use this? While clicking on the hover over element or the next element that is displayed in a list?
you can use above code any time, it works even if the element is invisible.
Any other option than using a javascript? On another note, could you tell me what is the mistake/problem in the above java code?
The above javascript might not work in my scenario since the elements are accessible only by xPath and not ids. Ids are generated on the fly and therefore will not be used to locate an element
But this is only click on hidden url or link. Not open dropdown menu on mouse over. i need to open menu mouse over
|
5

Your Actions builder looks slightly wrong to me. Here is a example I use:

public static void mouseClickByLocator( String locator ) {    
  WebElement el = driver.findElement( By.cssSelector( locator ) );    
  Actions builder = new Actions(driver);    
  builder.moveToElement( el ).click( el );    
  builder.perform();    
}

1 Comment

@Igal - If this native action I show above doesn't work, then consider writing a non-native JavascriptExeucutor method to do the hover.
1
Actions builder = new Actions(driver);
builder.MoveToElement(menu).MoveToElement(submenu).Click().Perform();

It works under Chrome, but doesn't work in FF

Comments

0

You could try this:

  1. Get your WebElement by its xpath.
  2. Hover element.
  3. Get your WebElement by its xpath again.
  4. Click it.

It's because of element's id is changing when you hover over it and you should find it again.

Actions builder = new Actions(driver);

WebElement mainMenuBTN = getWebEl("xpath_string",5);
builder.moveToElement(mainMenuBTN).perform();
mainMenuBTN = getWebEl("xpath_string",5);
builder.click(mainMenuBTN);

I use this method to ipmlement controlled explicit wait into my elements' instantiation.

protected WebElement getWebEl(String xpath, int waitSeconds) {
    wait = new WebDriverWait(driver, waitSeconds);
    return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
}

Comments

0

My case we had a table of rows, if mouse over on the row, one of the column(td) should display some 4 icons, we should click on it.

Action action=new Action(driver);
action.moveToElement(hoverElt).clickAndHold().build().perform();

It worked for me. moveToELement() move your control to the element

clickAndHold() clicks and holds the hovered element, so that its easy for us to do operation on visible elements.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.