如何在另一个列表中的列表内单击链接

时间:2017-05-24 13:42:07

标签: java selenium selenium-webdriver automation automated-tests

我无法让驱动程序点击名为"出售"的链接文字。 我已经尝试了很多可能性,包括:

List <WebElement> eventLogList = driver.findElements(By.className("nav navbar-nav"));

这里是HTML代码:

<ul class="nav navbar-nav">
            <li class="dropdown open">
                <a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">event log<span class="caret"></span></a>
                <ul class="dropdown-menu" role="menu">
                    <li><a href="/scan/receive"><span class="text">income</span></a></li>
                    <li><a href="/scan/delivery"><span class="text">outcome</span></a></li>
                </ul>
            </li>

            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Consult<span class="caret"></span></a>
                <ul class="dropdown-menu" role="menu">
                    <li><a href="/report/trackable"><span class="text" id="trackable_report">buy</span></a></li>
                    <li><a href="/report/event"><span class="text" id="event">details</span></a></li>
                </ul>
            </li>

            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">serie number<span class="caret"></span></a>
                <ul class="dropdown-menu" role="menu">
                    <li><a href="/generate-ium"><span class="text" id="generate-ium">sell</span></a></li>
                    <li><a href="/generate-ium"><span class="text" id="generate-ium">resell</span></a></li>
                </ul>
            </li>

        </ul>

5 个答案:

答案 0 :(得分:2)

您可以尝试使用cssselector,如下所示:

 List <WebElement> eventLogList = driver.findElements(By.cssSelctor(".nav.navbar-nav"));

然后点击卖出使用:

 eventLogList.get(0).findElement(By.cssSelector("li:nth-child(3) ul>li>a")).click();

答案 1 :(得分:1)

By.className不允许使用多个类,只允许使用一个类。

你想要的是By.cssSelector

List <WebElement> eventLogList = driver.findElements(By.cssSelector(".nav .navbar-nav"));

答案 2 :(得分:1)

您可以直接尝试使用

点击链接
{{1}}

答案 3 :(得分:1)

我遇到了类似的问题。这是下拉菜单,在悬停/点击时打开菜单。在单击菜单项之前,您需要确保展开下拉菜单,并且菜单项在可见之前可见,直到它们被单击为止。当下拉菜单可见时,aria-expanded将设置为true

以下是我处理它的方式:

public boolean selectFromDropdown(String parentMenuText, String childMenuText) {
    boolean flag = false;
    // Get the nav bar
    WebElement navBar = driver.findElement(By.cssSelector(".nav.navbar-nav"));

    // Get all parent menu items
    List<WebElement> allNavItems = navBar.findElements(By.xpath(".//li[contains(@class,'dropdown')]"));

    // Loop through allNavItems to find the parentMenuText
    for(WebElement parentMenuItem : allNavItems) {
        WebElement parentMenuLink = parentMenuItem.findElement(By.xpath(".//a[contains(text(),'" + parentMenuItem + "')]"));
        String actualText = parentMenuLink.getText();
        if(actualText.equalsIgnoreCase(parentMenuText) {
            // if true, found parent menu item
            parentMenuLink.click();
            // When parentMenuLink is clicked, it will trigger a DOM update in the aria-expanded attributes and therefore we need to re-initialize the element to avoid StaleElementException
            parentMenuLink = parentMenuItem.findElement(By.xpath(".//a[contains(text(),'" + parentMenuItem + "')]"));
            if(parentMenuLink.getAttribute("aria-expanded").equals("true")) {
                //Get the child UL
                WebElement childMenuContainer = parentMenuItem.findElement(By.xpath(".//ul[@role='menu']"));

                // Get the child menu link
                WebElement childMenuLink = childMenuContainer.findElements(By.xpath(".//li/a/span[@class='text' and contains(text(),'" + childMenuText + "')]/.."));
                // Click on the child menu link
                childMenuLink.click();
                flag = true;
            }
        }
        if(flag) {
            break; // Break out of the loop if child menu link has been clicked
        }
    }
}

答案 4 :(得分:0)

问题解决了! 我已经在开发的开始尝试了这个解决方案,由于某种原因它不起作用。 经过一番尝试,下面的解决方案解决了我的问 代码如下:

        WebElement btnConsult= driver.findElement(By.xpath("//*[@id='bs-example-navbar-collapse-1']/ul[1]/li[2]/a"));
    btnConsult.click();

    WebElement btnDetails = driver.findElement(By.linkText("Details"));
    btnDetails.click();