kokodee
kokodee

Reputation: 305

Selenium WebDriver click on a dropdown after hovering the mouse on a menu

I am having trouble clicking on the New Events drop down menu resulting from hovering the mouse over a main menu option Events. The issue with the menu resulting from hovering the mouse is that the sub menu elements (All Events and New Events) are not available for selection until the drop down menu appears. When i run my script, the presence of the element cannot be located.

Below is a snippet of my HTML

<div id="navigation">
    <nav id="top-nav">
      <ul id="left-nav" class="left-navbar">
        <a class="no-hover" href="mainMenu.html">
        <li class="border-right">
          <a id="EventsMenu" href="eventsList.html">Events ▼</a>
            <ul class="submenu">
                <li>
                    <a id="ev.eventList.vadm" href="eventsList.html">All Events</a>
                </li>
               <li>
                   <a id="ev.newEventList.vadm" href="newEventsList.html">New Events</a>
               </li>
            </ul>
         </li>
       </ul>
    </nav>
</div>

And here is the part of the selenium script that fails (using PhantomJS):

WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='EventsMenu']")));  // locating the main menu

WebElement menu = driver.findElement(By.xpath("//*[@id='EventsMenu']"));
Actions builder = new Actions(driver); 
builder.moveToElement(menu).build().perform();

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='ev.newEventList.vadm']/tbody/tr[2]/td[1]/a[1]"))); 

WebElement menuOption = driver.findElement(By.xpath("//*[@id='ev.newEventsList.vadm']/tbody/tr[2]/td[1]/a[1]"));
menuOption.click();

Upvotes: 1

Views: 4778

Answers (1)

Rohhit
Rohhit

Reputation: 742

Try doing this and let me know if it works :

    WebDriverWait wait = new WebDriverWait(driver, 5);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='EventsMenu']")));  // locating the main menu

    WebElement menu = driver.findElement(By.xpath("//*[@id='EventsMenu']"));
    Actions builder = new Actions(driver); 
    builder.moveToElement(menu).build().perform();

    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='ev.newEventList.vadm']/tbody/tr[2]/td[1]/a[1]"))); 

    WebElement menuOption = driver.findElement(By.xpath("//*[@id='ev.newEventsList.vadm']/tbody/tr[2]/td[1]/a[1]"));
    builder.moveToElement(menuOption).click().build().perform();

Upvotes: 2

Related Questions