【问题标题】:Selenium Web Driver returns Selector not visibleSelenium Web Driver 返回 Selector 不可见
【发布时间】:2017-06-30 07:18:02
【问题描述】:

我有一个看起来像这样的 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 查找元素,并编写了以下 xpath。

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

但是我得到以下内容

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

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

【问题讨论】:

    标签: selenium xpath automation webdriver webautomation


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢@Guy 的解决方案,但我仍然不清楚是什么让驱动程序“等待”这个标签。如果您可以将我指向任何有解释的文档或帮助链接,那将非常有帮助。我是自动化方面的新手,我正在努力学习。再次感谢您的宝贵帮助。
    • @CaRtY5532 驱动程序并没有真正等待。它每 500 毫秒对 DOM 进行一次采样,并检查是否满足 expected condition,在这种情况下,它将尝试定位元素并检查它是否显示并具有高度和宽度(您可以在我附加的链接中看到它)。如果它在时间限制内成功(我的回答是 10 秒),它将返回元素。否则会抛出TimeoutException
    【解决方案2】:

    使用 WebDriverWait 查找元素。

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

    Check this post

    【讨论】:

      猜你喜欢
      • 2021-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 2013-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多