java:如何再次选择下拉列表后单击下拉列表中的所有选项

时间:2017-11-27 08:40:01

标签: java select drop-down-menu

我正在处理一个包含4个选项的下拉菜单,每次我在下拉菜单中展开(或点击)时都需要点击每个选项。我编写了第一次点击特定元素的逻辑,但是当我展开下拉列表时,代码无法单击下拉列表中的任何其他选项。请告诉我我在哪里做错了。

代码 -

try {
    Thread.sleep(3000);     

    // gets all the options in the dropdown.
    List<WebElement> select = driver.findElements(By.xpath("//*[@class='select2-results__option']"));
    System.out.println(select.size());
    for (int i=0; i<select.size();i++) {
        select.get(i).click();
        Thread.sleep(2000);
        // drodpdown element
        List<WebElement> ele = driver.findElements(By.xpath(GetPropertValue("dropdownvalue")));
        // expands the dropdown again.
        ele.get(1).click();
        Thread.sleep(2000);
        select.get(i).click();
    }

} catch (Exception e ){
    Assert.fail("unable to click option from services");
}

下拉代码段 -

&#13;
&#13;
<ul class="select2-results__options ps-container ps-theme-default" role="tree" id="select2-j_idt2339-results" aria-expanded="true" aria-hidden="false" data-ps-id="fcad5f76-4b37-a689-7d7c-d975c236d339">

<li class="select2-results__option" id="select2-j_idt2339-result-x9ct-022" role="treeitem" aria-selected="false">Issue License</li>

<li class="select2-results__option" id="select2-j_idt2339-result-wuzg-023" role="treeitem" aria-selected="false">Issue License from Old License</li>

<li class="select2-results__option" id="select2-j_idt2339-result-b9ni-024" role="treeitem" aria-selected="false">Replace License</li>

<li class="select2-results__option" id="select2-j_idt2339-result-zj1d-025" role="treeitem" aria-selected="false">Renew License</li>

<div class="ps-scrollbar-x-rail" style="left: 0px; bottom: 3px;">
    <div class="ps-scrollbar-x" tabindex="0" style="left: 0px; width: 0px;"></div>
</div>

<div class="ps-scrollbar-y-rail" style="top: 0px; right: 3px;">
    <div class="ps-scrollbar-y" tabindex="0" style="top: 0px; height: 0px;"></div>
</div>

</ul>​
&#13;
&#13;
&#13;

我添加了以下代码,但未能点击下拉列表。

try {
    Thread.sleep(3000);

    WebElement dropdown = driver.findElement(By.xpath("//*[@class=' form-field ']/span[1]/span[1]/span"));

    List<WebElement> list = driver.findElements(By.xpath("//*[@role='tree']/li"));
System.out.println(list.size());
 Thread.sleep(5000);
    for(int i=1;i<list.size();i++) {
         Thread.sleep(5000);
       dropdown.click();
       Thread.sleep(3000);
        list.get(i).click();
   Thread.sleep(5000);

        System.out.println("Selected : " +list.get(i).getText());
    }

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码:

WebElement dropdown = driver.findElement(By.xpath("//ul[@id='select2-j_idt2339-results']"));
    List<WebElement> options = driver.findElements(By.xpath("//ul[@id='select2-j_idt2339-results']/li"));
    for(int i=1;i<options.size();i++) {
         dropdown.click();
         Thread.sleep(3000);
        options.get(i).click();
        Thread.sleep(3000);
        System.out.println("Selected : " +options.get(i).getText());
    }

选择下拉选项的另一种方法(您也可以使用selectbyvisibletext或selectbyvalue而不是selectbyindex):

Select dropdown = new Select(driver.findElement(By.xpath("<Xpath of Dropdown>")));
for(int i=0;i<=dropdown.getOptions().size();i++) {
    dropdown.selectByIndex(i);
    Thread.sleep(3000);
    }