在selenium Webdriver的bootstrap下拉菜单中选择选项

时间:2016-04-19 16:18:32

标签: java css selenium selenium-webdriver

我试图点击下拉菜单中的特定行,但我的xpath或CSS无法识别对象...你的xpath或CSS会是什么?你的帮助将不胜感激。谢谢!

<div id="WBnavbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" aria-expanded="false" aria-haspopup="true" role="button" data-toggle="dropdown" href="#">
Renew 
<span class="caret"/>
</a>
<ul class="dropdown-menu scrollable-menu" role="menu">
<li>
<a onclick="" href="link1">Renew option1</a>
</li>
<li>
<a onclick="" href="link2">Renew option2</a>
</li>
<li>
<a onclick="" href="link3">Renew option3</a>
</li>
<li class="dropdown">
<a class="dropdown-toggle" aria-expanded="false" aria-haspopup="true" role="button" data-toggle="dropdown" href="#">
Update Address 
<span class="caret"/>
</a>
<ul class="dropdown-menu scrollable-menu" role="menu">
<li>
<a onclick="" href="link4">Update Address Option1</a>
</li>
<li>
<a onclick="" href="link5">Update Address Option2</a>
</li>
<li>
<a onclick="" href="link6">Update Address Option3</a>
</li>

2 个答案:

答案 0 :(得分:0)

这对你有用。

    public void selectValue(WebElement list, final String value) {
        List<WebElement> options = list.findElements(By.tagName("li"));

        for (WebElement option : options) {
            if (option.getText().contains(value)) { //Here you have select contains or some thing 
                option.click();
                break;
            }
        }
    }

WebElement ul = driver.findElement(By.className("dropdown-menu"));
selectValue(ul, "Option1");

仅适用于输入类型=选择 创建WebElement并将其转换为Select元素(org.openqa.selenium.support.ui.Select),使用Select方法选择您的选项。这仅适用于html选择元素。

Select element = new Select(webelement);
element.selectByValue(value);

答案 1 :(得分:0)

以下是使用Selenium 3.141.59的示例。这是用于Bootstrap's下拉按钮的。我想这在这种情况下也可能会有所帮助。

我注意到,当鼠标不在浏览器上方时,这种方法会更好

    WebDriverWait webDriverWait = new WebDriverWait(driver, 15); //Use WebdriverWait to give the drop down menu time to display
    WebElement selectIssnIsbnButton = webDriverWait.until(ExpectedConditions.elementToBeClickable(By.className("evt-searchTypeButton")));//Find the button assoicated with the dropdown menu.
    selectIssnIsbnButton.click(); //Click that button
    WebElement list = webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.className("evt-searchType"))); //Wait until the dropdown list items are visible. This finds the list <ul>
    List<WebElement> menuItems = list.findElements(By.tagName("a"));//This finds all the <a> in the list
    System.out.println("size: " + menuItems.size());

    //loop through the <a> and click the one need. Then break.
    for (WebElement item : menuItems) {
        System.out.println("item: " + item.getText());
        if (item.getText().equals("ISSN/ISBN")) {
            item.click();
            break;
        }
    }
  

第二种解决方案

WebDriverWait webDriverWait = new WebDriverWait(driver, 20);
    WebElement selectIssnIsbnButton = webDriverWait.until(ExpectedConditions.elementToBeClickable(By.className("evt-searchTypeButton")));
    selectIssnIsbnButton.click();
    List<WebElement> list = webDriverWait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//ul[contains(@class,'evt-searchType')]//li//a")));  //driver.findElements(By.xpath("//ul[contains(@class,'evt-searchType')]//li//a"));
    System.out.println("list size: " + list.size());
    for (int i = 0; i < list.size(); i++) {
        System.out.println("dropdown: " + list.get(i).getText());
        if (list.get(i).getText().equals("ISSN/ISBN")) {
            list.get(i).click();
            break;
        }
    }