Selenium Select - Selecting dropdown option by part of the text

前端 未结 8 1904
清歌不尽
清歌不尽 2020-12-19 16:57

The class Selenium Select has 3 methods of different option selection:

  1. selectByIndex
  2. selectByValue
  3. selectByVisibleText

Now,

8条回答
  •  醉话见心
    2020-12-19 17:22

    Using Java 8 Stream/Lambda:

    protected void selectOptionByPartText(WebElement elementAttr, String partialText) {
            Select select = new Select(elementAttr);
            select.getOptions().parallelStream().filter(option -> option.getAttribute("textContent").toLowerCase().contains(partialText.toLowerCase()))
                    .findFirst().ifPresent(option -> select.selectByVisibleText(option.getAttribute("textContent")));
        }
    

提交回复
热议问题