2017-02-12 67 views
1

我有一个看起来像这样的HTML页面。我必须点击一个菜单下拉菜单并点击它的第6项。Selenium Web驱动程序返回选择器不可见

<li class="open"> 
    <a href="#" class="dropdown-toggle "> 
     <i><img src="/cs/images/icon_01.jpg" alt=""></i> 
     <span class="menu-text"> User Account Management </span> 

     <b class="arrow fa fa-angle-down"></b> 
    </a> 
    <b class="arrow"></b> 

    <!-- Account Analysis start --> 
    <ul class="submenu nav-show active" id="useraccount" style="display: block;"> 
     <li class=""> 
      <a href="/cs/servlets/UserServlet?action=newAccountAnalysis&amp;isLogged=true"> 
       <i class="menu-icon fa fa-caret-right"></i> 
       <span class="menu-text">Account Analysis</span> 
      </a> 
      <b class="arrow"></b> 
     </li> 
     <!-- Account Analysis end --> 
     <!-- Delete User Account start --> 
     <li class=""> 
      <a href="/cs/servlets/UserServlet?action=selectUserAccountToDelete&amp;isLogged=true"> 
       <i class="menu-icon fa fa-caret-right"></i> 
       <span class="menu-text">Delete User Account</span> 
      </a> 

      <b class="arrow"></b> 
     </li> 
     <!-- Delete User Account end--> 
     <!-- Unlock/Re-activate User Account  start -->  
     <li class=""> 
      <a href="/cs/servlets/UserServlet?action=selectUserAccountToUnlock&amp;isLogged=true"> 
       <i class="menu-icon fa fa-caret-right"></i> 
       <span class="menu-text">Unlock/Re-activate User Account</span> 
      </a> 

      <b class="arrow"></b> 
     </li> 
     <!-- Unlock/Re-activate User Account end --> 
     <!-- De-activate User Account  start -->  
     <li class=""> 
      <a href="/cs/servlets/UserServlet?action=selectUserAccountToDeactivate&amp;isLogged=true"> 
       <i class="menu-icon fa fa-caret-right"></i> 
       <span class="menu-text">De-activate User Account</span> 
      </a> 

      <b class="arrow"></b> 
     </li> 
     <!-- De-activate User Account end --> 
     <!-- Update User Profile start -->  
     <li class=""> 
      <a href="/cs/jsp/user/rsdUpdateUser.jsp"> 
       <i class="menu-icon fa fa-caret-right"></i> 
       <span class="menu-text">Update User Account</span> 
      </a> 

      <b class="arrow"></b> 
     </li> 
     <!-- Update User Profile end --> 
     <!-- Search RSD User start -->  
     <li class=""> 
      <a href="/cs/servlets/UserServlet?action=searchRsdUser&amp;isLogged=true"> 
       <i class="menu-icon fa fa-caret-right"></i> 
       <span class="menu-text">Search RSD User</span> 
      </a> 

      <b class="arrow"></b> 
     </li> 
     <!-- Search RSD User end --> 
    </ul> 
</li> 

,我需要选择项目“搜索RSD用户”,为此我决定用找到的XPath元素,我写了下面的XPath。

driver.findElement(By.xpath("//*[@id=\"useraccount\"]/li[6]/a")).click();

但是我得到以下

error.org.openqa.selenium.ElementNotVisibleException: element not visible 

我验证了,如果我的路径是由Chrome的开发者工具测试的XPath正确和路径工作正常,但是它不工作的webdriver。

回答

1

您可以使用显式的等待点击它

WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"useraccount\"]/li[6]/a"))); 
element.click(); 
+0

非常感谢您的解决方案@Guy,但我仍然不清楚驱动程序是否“等待”此标签。如果您可以指向我的任何文档或帮助有解释的链接,这将是非常有用的。我是自动化方面的新手,我正在努力学习。再次感谢您的宝贵帮助。 – CaRtY5532

+0

@ CaRtY5532驱动程序并未真正等待。它每500毫秒对DOM进行一次采样,并检查[预期条件](https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#visibilityOfElementLocated- org.openqa.selenium.By-),在这种情况下,它会尝试定位元素并检查它是否显示并具有高度和宽度(您可以在我附加的链接中看到它)。如果它在时间限制内成功(我的答案为10秒),它将返回元素。否则它会抛出'TimeoutException'。 – Guy

0

使用WebDriverWait找到元素之前,以确保该元素是可见的。

WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut); WebDriverWait等待=新的WebDriverWait(驱动程序,20); wait.until(ExpectedConditions.VisibilityofElementLocated(By.xpath(“// * [@ id = \”useraccount \“]/li [6]/a”)));

Check this post

相关问题