Stas S Stas S - 1 year ago 77
Java Question

Selenium Select - Selecting dropdown option by part of the text

Selenium Select have 3 methos of option selection:
1) selectByIndex
2) selectByValue
3) selectByVisibleText

Now, i have a situation now where i want to select an option by some text that partially appear in one of the options visible text (don't want to expose myself to changes in the WHOLE text).
For example:

<option value="0" label="not-intresting">VERY-LONG-TEXT-THAT-I-NEED-TO-SELECT-DOLLAR</option>

And i want to select this option only by providing the "DOLLAR", something like:


How would you implement it effectively?

Answer Source

You can try a logic like this hope this helps

    List <WebElements> optionsInnerText= driver.findElements(By.tagName("option"));

     for(WebElement text: optionsInnerText){

    String textContent = text.getAttribute("textContent");