org.openqa.selenium.support.ui.UnexpectedTagNameException:元素应该是“select”但是“title”

时间:2017-03-16 13:20:34

标签: java selenium selenium-webdriver webdriver

我的应用程序有下拉列表,显示批处理运行的时间 <span class="select2-selection__rendered" id="select2-siTime-container" title="10 AM - 11 AM">10 AM - 11 AM</span> 点击下拉列表旁边的下拉列表后,我将获得要选择的值。

<ul class="select2-results__options ps-container ps-theme-default ps-active-y" role="tree" id="select2-siTime-results" aria-expanded="true" aria-hidden="false" data-ps-id="e0656a98-863d-cab8-ceca-763253acd3a0">
  <li class="select2-results__option" id="select2-siTime-result-sl6p-TIME_6" role="treeitem" aria-selected="false">6 AM - 7 AM</li>
  <li class="select2-results__option" id="select2-siTime-result-ogb9-TIME_7" role="treeitem" aria-selected="false">7 AM - 8 AM</li>
  <li class="select2-results__option" id="select2-siTime-result-wx7b-TIME_8" role="treeitem" aria-selected="false">8 AM - 9 AM</li>

当我尝试选择下拉列表时,我看到错误

  

元素应该是select但是标题

我的代码:

Select mycombo = new Select(driver.findElement(By.tagName("title")));
driver.findElement(By.xpath("//*[@aria-labelledby='select2-siTime-container']/span[2]")).click();
Thread.sleep(8000);
mycombo.selectByIndex(4);

1 个答案:

答案 0 :(得分:2)

Select类只能用于<select>标记。要从此下拉列表中选择一个选项,请单击该选项,然后单击该选项。像

这样的东西
WebElement dropdown = driver.findElement(By.id("select2-siTime-results"));
dropdown.click();
List<WebElement> options = dropdown.findElements(By.className("select2-results__option"));
options.get(4).click();