I want to select a drop menu item from below drop menu in a web page:

Drop menu with menu items

HTML code for the drop menu

HTML code for menu items

I tried the following 3 methods to select item with text "Test" (Index[1]):

  • Case 1:
   driver.findElement(By.id("combo-1781-inputEl")).click();

   driver.findElement(By.id("combo-1781-inputEl")).sendKeys("Test");

No error appeared(Test passed). But "Test" item text was not displayed on the drop menu field as selected.

  • Case 2:
     driver.findElement(By.id("combo-1781-inputEl")).click();

     Select drp = new Select(driver.findElement(By.id("boundlist-1930-listEl")));
     drp.selectByVisibleText("Test");

org.openqa.selenium.support.ui.UnexpectedTagNameException was thrown. Test failed.

  • Case 3:
    Select drp = new Select(driver.findElement(By.id("boundlist-1930-listEl")));
    drp.selectByIndex(1);

org.openqa.selenium.support.ui.UnexpectedTagNameException was thrown. Test failed.

2 Answers

2
Sameer Arora On Best Solutions

You can first click on the dropdown element using:

driver.findElement(By.id("combo-1781-inputEl")).click();

And then you can click on the "Test" element using:

driver.findElement(By.xpath("//div[@id='boundlist-1930-listEl']//li[text()='Test']")).click();

And you can also parameterised the "Test" text here, so that in future if you want to click on any other dropdown value, you can just send that text in the xpath and then click it, you can do it like:

String text="Test";
driver.findElement(By.xpath("//div[@id='boundlist-1930-listEl']//li[text()="+text+"]")).click();
1
S Ahmed On

As seen your code, the element with id boundlist-1930-listEl is not a <select> node. That is why the UnexpectedTagNameException was thrown.

To select the items from this kind of drop-down you need to make the drop-down list visible first, I'm assuming by clicking on the <input> element. Then you have to wait for the list to show up and then click on the item you want to select.

Try this:

WebDriverWait wait = new WebDriverWait(driver,30);

driver.findElement(By.id("combo-1781-inputEl")).click();

wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("div#bounddlist-1930-list > ul"))));

driver.findElement(By.xpath("//div[@id='bounddlist-1930-list']/ul/li[contains(text(),'Test')]")).click();