1

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. I would thank any help on this.


enter image description here

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

Your locator seems wrong.

Try:

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

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, privacy policy and cookie policy

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