单击Selenium Java列表中的元素

时间:2016-01-12 12:37:28

标签: selenium

如何点击以下代码中的元素“one”:

        <div class="navbar navbar-default">
    <ul class="nav navbar-nav">
    <li><a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0);" id="Tiles">cat1 <b class="caret"></b></a>
    <ul class="dropdown-menu">
    <li><a id="Wid" href="javascript:void(0);">one</a></li>
    <li><a id="Sett" href="javascript:void(0);">two</a></li>
    <li><a id="Th" href="javascript:void(0);">three</a></li></ul></li>
    <li><a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0);" id="Groups">Cat2 <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a id="Events" href="javascript:void(0);">four</a></li>
<li><a id="Folder" href="javascript:void(0);">five</a></li>
<li><a id="Cat" href="javascript:void(0);">six</a></li>
<li class="disabled"><a id="Procedure" href="javascript:void(0);">Procedure</a></li></ul></li></div>

感谢:)

2 个答案:

答案 0 :(得分:0)

“one”不是元素,因此您只能找到其父<a>元素:

By.Id("Widgets")

答案 1 :(得分:0)

根据我的理解,&#34;一个&#34;是下拉列表中的一个值,对吗?如果是这样,您可以在类中命名之后使用以下方法之一查找下拉列表和特定值:

public void selectFromDropDownListFoundById(String detailIDIdentifier, String text, String value) {
        Select droplist = new Select(driver.findElement(By.id(detailIDIdentifier)));
        // Choose to Select by Text or by Value of the drop-down list
        if (text == null && value != null) {
            droplist.selectByValue(value);
        } else if (text != null && value == null) {
            droplist.selectByVisibleText(text);
        } else {
            throw new AssertionError("Cannot be both parameters null. Give text or value");
        }
}

public void selectFromDropDownListFoundByName(String detailIdIdentifier, String text, String value) {
        Select droplist = new Select(driver.findElement(By.name(detailIdIdentifier)));
        // Choose to Select by Text or by Value of the drop-down list
        if (text == null && value != null) {
            droplist.selectByValue(value);
        } else if (text != null && value == null) {
            droplist.selectByVisibleText(text);
        } else {
            throw new AssertionError("Cannot be both parameters null. Give text or value");
        }
    }