The class Selenium Select has 3 methods of different option selection:
Now,
Eventually I combined the answers here and that's the result:
Select select = new Select(driver.findElement(By.xpath("//whatever")));
public void selectByPartOfVisibleText(String value) {
List optionElements = driver.findElement(By.cssSelector("SELECT-SELECTOR")).findElements(By.tagName("option"));
for (WebElement optionElement: optionElements) {
if (optionElement.getText().contains(value)) {
String optionIndex = optionElement.getAttribute("index");
select.selectByIndex(Integer.parseInt(optionIndex));
break;
}
}
Thread.sleep(300);
}
And in Scala (need it eventually in Scala) it looks like:
def selectByPartOfVisibleText(value: String) = {
val optionElements: util.List[WebElement] = selectElement.findElements(By.tagName("option"))
breakable {
optionElements.foreach { elm =>
if (elm.getText.contains(value)) {
val optionIndex = elm.getAttribute("index")
selectByIndex(optionIndex.toInt)
break
}
}
}
Thread.sleep(300)
}