2

I'm trying to select an option from a drop-down list, and I'm not able using the common methods of the Select class.

I also tried the selectByVisibleName and selectByValue and same result.

Select oSelect = new Select(driver.findElement(By.xpath("//*[@model='typeOfCover']")));
oSelect.selectByIndex(2);

Here is the piece of HTML of this drop-down list.
enter image description here

Any ideas?

1
  • 1
    I strongly recommend that you use a list of web elements. List WebElement You have more flexibility and its a better practice Commented Jul 12, 2019 at 18:27

3 Answers 3

2

Your locator seems wrong.

Try:

Select menu = new Select(driver.findElement(By.cssSelector("ul.dropdown-menu")))
2

I fail to see any HTML select tag in the code you provided hence you will not be able to use Select class

My expectation is that you need to amend you XPath locator to match the element by data-original-index attribute, something like:

WebElement secondValue = driver.findElement(By.xpath("//li[@data-original-index='2']/a"));
secondValue.click();

More information on using XPath:

0
  1. Try debugging your code in the IDE.
  2. Or try your XPath selector in the Chrome dev tools elements tab, hit Ctrl + F and you can test your XPath, CSS, etc. See what that selector is actually finding.

It looks like you do need to click that button first and then select from the UL.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.