Selenium WebDriver将鼠标悬停在菜单上后单击下拉列表

时间:2017-07-21 17:06:59

标签: java selenium drop-down-menu click

我无法点击将鼠标悬停在主菜单选项“事件”上的“新事件”下拉菜单。悬停鼠标导致的菜单问题是,在出现下拉菜单之前,子菜单元素(所有事件和新事件)无法选择。当我运行我的脚本时,无法找到元素的存在。

以下是我的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>

以下是selenium脚本失败的部分(使用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();

1 个答案:

答案 0 :(得分:1)

尝试这样做,让我知道它是否有效:

    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();