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?
-
1I strongly recommend that you use a list of web elements. List WebElement You have more flexibility and its a better practiceuser3269676– user32696762019年07月12日 18:27:52 +00:00Commented Jul 12, 2019 at 18:27
3 Answers 3
Your locator seems wrong.
Try:
Select menu = new Select(driver.findElement(By.cssSelector("ul.dropdown-menu")))
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:
- Try debugging your code in the IDE.
- 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.