0

I'm at my wits end here. I'm assuming I'm doing something wrong but I cannot for the life of me figure out what.

I have a navbar I'm trying to hover over the main element, which causes the drop down to expand, and then click on a child element in IEx86. This seems simple, right?

HTML (generic-ed up):

<nav class="navHeader">
 <ul class="topNavigation">
  <li class="firstItem"><a href="/Home">Home</a></li>
  <li class="current"><a href="javascript:void(0)">Transactions</a>
   <ul style="display: block;">
    <li class=""><a class="tabbedTransactions" href="/Transaction/#li1">li1</a></li>
    <li class=""><a class="tabbedTransactions" href="/Transaction/#li2">li2</a></li>
    <li class=""><a class="tabbedTransactions" href="/Transaction/#li3">li3</a></li>
    <li class=""><a class="tabbedTransactions" href="/Transaction/#li4">li4</a></li>
    <li class=""><a class="tabbedTransactions" href="/Transaction/#li5">li5</a></li>
    <li class="current bottomSubNav"><a href="/li6/" class="current bottomSubNav">li6</a></li>
   </ul>
  </li>
 </ul>
</nav>

What I thought would work (and does if I hover my ACTUAL cursor over the Transaction element while the code is running) (WebElements are all found via xpath and passed in, xpath has been verified using a plugin) (driver is WebDriver and a public part of the class)

public void HoverClick(WebElement hover, WebElement click)
{   
    Actions actions = new Actions(driver);
    actions.moveToElement(hover).click(click).build().perform();
}

The .moveToElement does not appear to move to so therefore there can be no click. (not even a flicker of the navBar item expanding)

public void HoverClick(WebElement hover, WebElement click)
{   
    Actions actions = new Actions(driver);
    actions.moveToElement(hover).build().perform();
    actions.click(click).build().perform();
}

With this one, moveToElement moves to (the navBar expands) but .click(click) fails. Leaving off the .perform() of the moveToElement causes no change in behavior.

public void HoverClick(WebElement hover, WebElement click)
{   
    Actions actions = new Actions(driver);
    actions.moveToElement(hover).build().perform();
    actions.pause(Duration.ofMillis(1000)).build().perform();
    actions.click(click).build().perform();
}

With this one, the first time the .pause() is .build().perform()-ed there IS a pause. The 2nd time (with .click().build().perform()) there is no pause.

I don't know what to do from here. As stated, if I hover my actual mouse of the nav element then the code executes just fine. Please help?

  • I typically use `Actions` to do the hover but just do a normal `element.click()`. Have you tried that? Have you tried adding a wait before the hover? Are you confident that it's present and hoverable at the point the code runs? One way to confirm this is to put a break point right before `HoverClick()` is called. Once it breaks, step through the code one line at a time and see if it works. – JeffC Dec 13 '17 at 23:22
  • Taking your advice it looks like the following works: `actions.moveToElement(hover).perform();` `click.click();` Why that would work and `.build().perform()` doesn't confuses me. – Dan Gentry Dec 14 '17 at 05:00
  • Well, with some caveats. Please see my "Answer" and the associated comment? Please? :) – Dan Gentry Dec 14 '17 at 17:34

1 Answers1

0

The following works:

public void HoverClick(WebElement hover, WebElement click)
{   
    Actions actions = new Actions(driver);
    actions.moveToElement(hover).perform();
    click.click();
}

using .build().perform() did not work. This appeared to cause the "mouse" to hover once, then hover a second time and move away.

Using just .build() did not work at all (not surprised, but wanted to check #QALife). The original did not work at all. This seems like a significant issue in Selenium...

  • If anyone reads this. this code is behaving VERY inconsistently. Sometimes it works great, sometimes it does not work AT ALL. If I put a `Thread.Sleep(500);` between the Actions and the Click it NEVER works. I've resorted to putting a 5s sleep between the HoverClick and any next step and that STILL doesn't perform consistently. I can see the elements on the page and it just does not perform consistently. Any advice would be appreciated. – Dan Gentry Dec 14 '17 at 16:51
  • What if you put a pause before the hover? Does that make things more consistent? I'm wondering if the element you are hovering is not in a consistent state when that part of the script is executed. – JeffC Dec 14 '17 at 18:32
  • Another option is to just use Javascript to click the menu item without the hover. I try to avoid that whenever possible because it's not something that a user would do but sometimes it's the only thing you can get to work. – JeffC Dec 14 '17 at 18:58
  • By switching to `.cssSelector` from `.xpath` seems to have helped... I'm getting "Clean run" then "Element Not Clickable" failures. – Dan Gentry Dec 14 '17 at 19:51
  • If you are in fact locating the same element in both, CSS selector vs XPath really should be any different. I'm guessing you just got lucky. – JeffC Dec 14 '17 at 20:04
  • From my research it appears that xpath is somewhat unreliable and xpath in IE is even more so because some sort of custom xpath implementation via the IEDriver. At least according to: https://stackoverflow.com/questions/16788310/what-is-the-difference-between-css-selector-xpath-which-is-betteraccording-t/16794683#16794683 – Dan Gentry Dec 14 '17 at 20:08
  • Yes, my locator preference is 1. ID, 2. CSS selector. If I need to find an element by the text it contains or do DOM traversal, I use XPath. In general, I rarely need to use XPaths. Most of my locators are CSS selectors but that will depend on your site and how many IDs are on the desired elements. – JeffC Dec 14 '17 at 20:14