Reputation: 4097
I am having one of those scenarios with Selenium where it sometimes works.
The flow I’m focusing on is pretty simple and this has been asked and answered several times.
Hover over main menu item Move to and click on sub menu item.
Here is the code:
Any advice as to how I could make this work every time would be most appreciated.
var actions = new Actions(SeleniumTestDriver.WebDriver);
// Move to the Main Menu Element and hover
actions.MoveToElement(SeleniumTestDriver.WebDriver.FindElement(By.XPath(@"//*[@id='main_menu']/ul/li[3]/a"))).Perform();
Thread.Sleep(1000);
var wait = new WebDriverWait(SeleniumTestDriver.WebDriver, TimeSpan.FromSeconds(5));
var subMenuLink = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(@"//*[@id='main_menu']/ul/li[3]/ul/li[4]/a")));
Thread.Sleep(250);
actions.MoveToElement(subMenuLink).Click().Perform();
Version Details: Firefox v33.1 Selenium.WebDriver 2.44.0 Selenium.Support 2.44.0 Language C#
Edit by op
I should also add that when it fails, the menu flashes, as if the hover has been interrupted. Opens and shuts too quickly for the sub-menu item to be clicked.
The Html for that menu is:
<nav id="main_menu" class="ddsmoothmenu">
<ul class="primary_menu">
<li><a href="/webinar/calendar">calendar</a></li>
<li class="parent" style="z-index: 100;">...</li>
<li class="parent" style="z-index: 99;">
<a href="javascript:void(0)" class="">Upcoming Webinars<i></i></a>
<ul style="top: 95px; visibility: visible; left: 0px; width: 195px; display: none;">
<li role="presentation">
<a role="menuitem" tabindex="-1" href="/Webinar/Details/4567">Best-Ever Compliance Checklists For...</a>
</li>
<li role="presentation">
<a role="menuitem" tabindex="-1" href="/Webinar/Details/4572">Build a No-Excuses Sales Environmen...</a>
</li>
<li role="presentation">
<a role="menuitem" tabindex="-1" href="/Webinar/Details/4560">Handling Power of Attorney Document...</a>
</li>
<li role="presentation">
<a role="menuitem" tabindex="-1" href="/Webinar/Details/4566">Flood Insurance</a>
</li>
<li role="presentation">
<a role="menuitem" tabindex="-1" href="/Webinar/Details/4562">Opening Accounts for Nonresident Al...</a>
</li>
<li role="presentation">
<a role="menuitem" tabindex="-1" href="/Webinar/Details/4561">New Share Member Account Interview ...</a>
</li>
<li role="presentation">
<a role="menuitem" tabindex="-1" href="/Webinar/allActive/?eventsToShow=upcoming">
<font color="green">View <b>All</b> Upcoming Events</font>
</a>
</li>
</ul>
</li>
<li class="parent" style="z-index: 98;">...</li>
<li style="z-index: 97;">
<a href="javascript:void(0)" class="">About Us<i></i></a>
<ul style="display: none; top: 95px; visibility: visible;">
<li role="presentation"><a role="menuitem" tabindex="-1" href="/Home/WhatIsAWebinar">What Is A Webinar?</a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="/Home/CommonQuestions">Commonly Asked Questions</a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="/Home/DetailedConnectionInstructions">Connecting to Your Webinar</a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="/Home/contactus">Contact Us</a></li>
</ul>
</li>
</ul>
</nav>
Thanks
Upvotes: 0
Views: 2981
Reputation: 3649
please try it out with below code:
WebElement web = dri.findElement(By
.xpath("//a[contains(.,'Upcoming Webinars')]"));
Actions objA = new Actions(dri);
objA.moveToElement(web).click().build().perform();
objA.moveToElement(
(new WebDriverWait(dri, 3)).until(ExpectedConditions.elementToBeClickable(By
.xpath("//a[contains(text(),'Safe Deposit Boxes')]"))))
.click().build().perform();
You cab replace Safe Deposit Boxes
with your own choice of element to be clicked.
P.S. Sorry but this code is in java, please help yourself for any equivalent in C#.
Upvotes: 0
Reputation: 4424
I have modified the xpath of your existing code. Please check if that works for you:
var actions = new Actions(SeleniumTestDriver.WebDriver);
// Move to the Main Menu Element and hover
actions.MoveToElement(SeleniumTestDriver.WebDriver.FindElement(By.XPath("//li[@class='parent']/a[.='Upcoming Webinars']"))).Build().Perform();
var wait = new WebDriverWait(SeleniumTestDriver.WebDriver, TimeSpan.FromSeconds(10));
var subMenuLink = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//li[@role='presentation']/a[contains(text(),'Flood Insurance')]")));
actions.MoveToElement(subMenuLink).Click().Perform();
Upvotes: 1