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?

Alex Kulinkovich
6272 gold badges8 silver badges18 bronze badges
asked Jul 12, 2019 at 18:10
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")))
answered Jul 13, 2019 at 20:58
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:

answered Jul 14, 2019 at 9:45
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.

Bulat
3003 silver badges9 bronze badges
answered Jul 14, 2019 at 23:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.