如何从selenium java中的multi_select中的列表中选择项目

时间:2018-05-21 10:39:56

标签: java selenium-webdriver

我的尝试:

Select listbox = new Select(
       driver.findElement(By.xpath("//*[@id='multiselect_categories']"))
);
listbox.selectByValue("ATM");

某些选项选择时的Html代码:

<input name="multiselect_categories" id="multiselect_categories" 
       type="text" autocomplete="off" placeholder="Select option" 
       tabindex="0" class="multiselect__input" style="display: none;">

<div class="multiselect__tags">
  <div class="multiselect__tags-wrap" style="">
    <span class="multiselect__tag">
      <span>Actions and Practices</span>
      <i aria-hidden="true" tabindex="1" class="multiselect__tag-icon"></i>
    </span>
    <span class="multiselect__tag">
      <span>Air Carrier Services and Safety Oversight</span>
      <i aria-hidden="true" tabindex="1" class="multiselect__tag-icon"></i>
    </span>
  </div>
  <div class="multiselect__spinner" style="display: none;"></div>
  <input name="multiselect_categories" id="multiselect_categories" 
         type="text" autocomplete="off" placeholder="Select option" 
         tabindex="0" class="multiselect__input" 
         style="width: 0px; position: absolute; padding: 0px; display: none;">  
</div>

<div class="multiselect__content-wrapper" style="max-height: 291.375px; display: none;">
  <ul class="multiselect__content" style="display: inline-block;">
    <li class="multiselect__element">
      <span data-select="Press enter to select" data-selected="Selected" 
            data-deselect="Press enter to remove" class="multiselect__option">
        <span>ATM</span>
      </span> 
    </li>
    <li class="multiselect__element">
      <span data-select="Press enter to select" data-selected="Selected" 
            data-deselect="Press enter to remove" class="multiselect__option 
            multiselect__option--selected">
        <span>Actions and Practices</span>
      </span> 
    </li>
    <li class="multiselect__element">
      <span data-select="Press enter to select" data-selected="Selected" 
            data-deselect="Press enter to remove" class="multiselect__option 
            multiselect__option--selected">
        <span>Air Carrier Services and Safety Oversight</span>
      </span>
    </li>
  </ul>
</div>

添加到testng时添加到selenium代码时失败的代码:

@Test(description = "Test5")
public void chooseCatagory(String... catagories) {
    for(String catagory: catagories) {
        // input catagory in text box which display placeholder `Select option`
        driver.findElement(By.cssSelector("div.multiselect__tags #multiselect_categories"))
            .sendKeys(catagory);
        // find the item from auto-suggest list
        driver.findElement(By.cssSelector("div.multiselect__tags + div > ul"))
            .findElement(By.xpath("./li//span[text()='"+catagory+"']"))
            .click();
    }
}
chooseCatagory("ATM", "Airports");

以上代码出错: org.testng.TestNGException: 无法使用[class [Ljava.lang.String;]注入@Test带注释的方法[chooseCatagory]。 有关本机依赖注入的更多信息,请参阅http://testng.org/doc/documentation-main.html#native-dependency-injection org.testng.TestNGException:

HTML没有选择时:

<input name="multiselect_categories" id="multiselect_categories" 

type="text" autocomplete="off" placeholder="Select option" tabindex="0" class="multiselect__input" style="display: none;">
<span><span class="multiselect__single">
            Select option
          </span></span>

列表包含的内容: ATM,动作,参考截图

1 个答案:

答案 0 :(得分:0)

@Test(description = "Test5")
public test_chooseCatagory() {
    chooseCatagory("ATM", "Airports");
}

private void chooseCatagory(String... catagories) {
    for(String catagory: catagories) {
        // click the down arrow at right to make the filter text box and
        // all option list display
        driver.findElement(By.cssSelector("div.multiselect__select"))
          .click();

        // input catagory into text box to filter matched options
        driver.findElement(By.cssSelector(".multiselect__tags #multiselect_categories"))
          .sendKeys(catagory);

        // click the option from filtered option list 
        driver.findElement(By.cssSelector(".multiselect__content-wrapper > ul"))
          .findElement(By.xpath("./li//span[text()='"+catagory+"']"))
          .click();

        // sleep 2 seconds before next choosing
        try {
         Thread.sleep(2000);
        }
        catch(Exception e) {
        } 
    } 
}