Selenium Web Driver返回Selector不可见

时间:2017-02-12 06:27:42

标签: selenium xpath automation webdriver webautomation

我有一个看起来像这样的HTML页面。我必须单击菜单下拉列表并单击其第6项。

<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的find元素,并编写了以下xpath。

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

但是我得到以下

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

我通过测试chrome的开发人员工具中的xpath来验证我的路径是否正确,并且路径工作正常但是它在webdriver上不起作用。

2 个答案:

答案 0 :(得分:1)

您可以使用显式等待来确保元素在单击之前可见

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

答案 1 :(得分:0)

使用WebDriverWait查找元素。

WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut); WebDriverWait wait = new WebDriverWait(driver,20); wait.until(ExpectedConditions.VisibilityofElementLocated(By.xpath( “// * [@ ID = \” useraccount \ “] /锂[6] / A”)));

Check this post