4

I have a simple CSS-based dropdown menu, and I'm trying to click on one of the menu items in a Java Selenium (WebDriver) test.

enter image description here

Both the menu (<ul> element) and the items (<a>) have IDs and creating corresponding WebElement objects works fine. I'm trying to click on one of the items with code like:

   hoverOver(transfersMenu);
   transferLink.click();

In hoverOver(), I've tried all three answers from this question, but none of them work. I keep getting:

org.openqa.selenium.ElementNotVisibleException: 
    Element is not currently visible and so may not be interacted with 
Command duration or timeout: 2.06 seconds

(I've tried calling transferLink.click() also before hoverOver(), in the hope that the implicit wait would make it work, but nope.)

Any idea how to make the hovering work so that the link can be clicked?

Selenium version 2.21.0. I'm running the tests on Linux (Ubuntu), using Firefox 13.0. A colleague just tried on Windows (using Firefox 12.0), and it didn't work for him either.

Update: As per Slanec's tip in comments, and these instructions, I tried setEnableNativeEvents(true) on the FirefoxProfile. At first this failed:

org.openqa.selenium.InvalidElementStateException: 
    Cannot perform native interaction: Could not load native events component.

...but after I upgraded to Selenium 2.23.1, I no longer get that complaint.

Still, the hovering doesn't work (with native events on or off). :-/

Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
  • The accepted solution from the linked question should work =/. What do you actually see when running the test? Does the `hoverOver()` open the list visually, or not? – Petr Janeček Jun 14 '12 at 09:41
  • Some users in different questions about related things also suggest toggling the native events on/off. – Petr Janeček Jun 14 '12 at 10:08
  • @Slanec: it doesn't open the menu visually. (The only way I ever got the test passing was to *manually* hover over the menu with mouse, which allowed Selenium to click on the menu item.) Hmm, I'll look into native events... – Jonik Jun 14 '12 at 10:59
  • @Slanec: did't get it working with native events either... see updated question. – Jonik Jun 14 '12 at 13:09

3 Answers3

0

I use the following code to hover over our menus for 1 second, before clicking a link, just like the one you are using:

action = new SeleniumActionHelper(driver);

WebElement currentUser = findElementByLinkText("testing1");
action.mouseHover(currentUser);
Thread.sleep(1000);

Of note, the mouse cursor needs to remain in the browser window for the hover to keep. If the mouse cursor is outside of the browser window, I experience a quick flash of the menu, but it does not stay visible

squeemish
  • 147
  • 5
  • 14
  • It should be mentioned that `SeleniumActionHelper` appears to be from a project called [PrimeFaces](http://code.google.com/p/primefaces/source/browse/examples/trunk/showcase/src/test/java/com/prime/showcase/integration/SeleniumActionHelper.java?r=6585) and what `mouseHover()` does is essentially [same as this](http://stackoverflow.com/a/6233282/56285). – Jonik Jun 21 '12 at 09:39
  • So the only difference compared to what I've already tried is `Thread.sleep(1000)` - I tried it but still doesn't work for me. Thanks anyway. – Jonik Jun 21 '12 at 09:40
0

Try this exampale:

WebElement menuHoverLink= driver.findElement(By.id("test"));
actions.moveToElement(menuHoverLink).perform();
driver.findElement(By.id("test")).click();
Thread.sleep(6000); 
RAS
  • 8,100
  • 16
  • 64
  • 86
testing
  • 1,736
  • 15
  • 46
  • 75
  • I'm no longer working on that product (or in that company), so I can't (easily) try your solution. Perhaps this helps others though. – Jonik Dec 04 '13 at 08:40
0

How do you run your test classes? I found out that running WebDriver through ANT makes hover actions impossible, whereas running the test classes from command line (TestNG JAR) or from Eclipse works just fine.

Hubert Grzeskowiak
  • 15,137
  • 5
  • 57
  • 74