0

Maybe somebody can help me figure this out. I want to be able to create a chain of Actions using java and Selenium webdriver. Here is what the source of the webpage looks like:

<li id="menu-item-14" class="menu-item menu-item-type-post_type menu-item-object-page   current-menu-item page_item page-item-5 current_page_item menu-item-has-children menu-item-14"><a href="http://www.somewebsite.com/about/">About</a>
<ul class="sub-menu" style="display: none; visibility: hidden;">
    <li id="menu-item-43" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-43"><a href="http://www.somewebsite.com/team/">Team</a></li>
</ul>
</li>

Basically when you hover over the About menu a submenu name Team will appear and I want to be able to select the submenu. This is what my code looks like

WebElement aboutMenu = _driver.findElement(By.id("menu-item-14"));
Actions builder = new Actions(_driver);
Action seriesofactions = builder
.moveToElement(aboutMenu)
.moveToElement(_driver.findElement(By.id("menu-item-43")))
.click()
.build();
seriesofactions.perform();

If I take the second moveToElement the code works just fine. Any ideas will be appreciated?

Update: Java Code used

WebElement menuHoverLink = _driver.findElement(By.id("menu-item-14"));
        actions.moveToElement(menuHoverLink).perform();
try {
            ((JavascriptExecutor)    _driver).executeScript("document.getElementByid('menu-item-14')).style.display='block'");
        } catch (Exception e) {
e.printStackTrace(); 
}
_driver.findElement(By.id("menu-item-43")).click();
Arthax
  • 33
  • 3
  • it works without that second move to element because you don't need to hover over to click something. it just needs to be clickable. – ddavison Feb 14 '14 at 21:07
  • also if i may make a suggestion: unless you are explicitly testing this, why not just force the url? `driver.get('http://.../whatever-url-that-menu-item-43-goes-to')` – ddavison Feb 14 '14 at 21:08
  • thanks but I'm explicitly trying to test it because further down I'll have to select a different submenu 3 tier deeper. And it appears that findElement is not able to get the menu item 43 after the first move. – Arthax Feb 15 '14 at 21:16

1 Answers1

2

When you hover over your menu-item-14, the style on the ul class="sub-menu" will change to style="display:block; and possibly remove the visibility from the styled element. That then allows the menu-item-43 to appear for selection.

See this post for more information on using mouse over hovers in selenium.

How to do mouse hover using Selenium WebDriver in Firefox 19?

Community
  • 1
  • 1
Ben
  • 667
  • 6
  • 13
  • That post seems to be in the right direction, I just not sure how to implement that on the java side – Arthax Feb 14 '14 at 18:47
  • The code for java should be relatively similar. Depending on which route you would like to go from the linked article, you can either use an actions builder to do it, or execute javascript to force the elements to become visible to interact with. – Ben Feb 14 '14 at 18:53
  • The code I used it always work for the Firefox driver but for like Chrome I seen it work like one time and then stop any ideas? – Arthax Feb 18 '14 at 16:52
  • In the Java code that you posted, you have .style.display='blocked'. Unfortunately, blocked isnt a property of style.display; 'block' is. See http://www.w3schools.com/jsref/prop_style_display.asp – Ben Feb 19 '14 at 21:39