Aditya
Aditya

Reputation: 505

org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "title"

My application have dropdown which shows timings of a batch run <span class="select2-selection__rendered" id="select2-siTime-container" title="10 AM - 11 AM">10 AM - 11 AM</span> After I click the dropdown beside the dropdown I will get values to select.

<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>

When I try to select dropdown I'm seeing error that

Element should have been select but was title

My Code:

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);

Upvotes: 0

Views: 9013

Answers (1)

Guy
Guy

Reputation: 50949

Select class can be used only for <select> tags. To select an option from this dropdown click on it and then click on the option. Something like

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();

Upvotes: 2

Related Questions