Selenium Select - Selecting dropdown option by par

2019-02-19 01:47发布

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

  1. selectByIndex
  2. selectByValue
  3. selectByVisibleText

Now, I have a situation 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:

select.selectByPartOfVisibleText("DOLLAR") 

How would you implement it effectively?

6条回答
我欲成王,谁敢阻挡
2楼-- · 2019-02-19 02:00

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");

    if(textContent.toLowerCase.contains(expectedText.toLowerCase))

           select.selectByPartOfVisibleText(expectedText);
    }
查看更多
beautiful°
3楼-- · 2019-02-19 02:00

Eventually I combined the answers here and that's the result:

public void selectByPartOfVisibleText(String value) {
    List<WebElement> 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.electByIndex(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)
  }
查看更多
做自己的国王
4楼-- · 2019-02-19 02:01

My solution is to use xpath to find options that are children of the select. Any xpath method can be used to find the select; in this example I am finding the select by id.

List<WebElement> options = driver.findElements(By.xpath("//select[@id = 'selectId')]/option"));

for (WebElement option : options) {
    if (option.getText().contains("DOLLAR")) {
        option.click();
        break;
    }
}

After a little more thought I realize the option can be found entirely with xpath:

driver.findElements(By.xpath("//select[@id = 'selectId')]/option[contains(text(), 'DOLLAR')]")).click();
查看更多
可以哭但决不认输i
5楼-- · 2019-02-19 02:09

Using Java 8 Stream/Lambda:

protected void selectOptionByPartText(WebElement elementAttr, String partialText) {
        Select select = new Select(elementAttr);
        driver.findElements(By.tagName("option"))
                .parallelStream()
                .filter(option -> option.getAttribute("textContent").toLowerCase().contains(partialText.toLowerCase()))
                .findFirst()
                .ifPresent(option -> select.selectByVisibleText(option.getAttribute("textContent")));
    }
查看更多
混吃等死
6楼-- · 2019-02-19 02:14

This is what I used to solve the issue in python.

I used this option, so it selects the text that uses the word DOLLAR instead of typing the whole word as the value.

Select(driver.find_element_by_name(ELEMENT)).select_by_value("DOLLAR")

instead of

Select(driver.find_element_by_name(ELEMENT)).select_by_visible_text(text)
查看更多
太酷不给撩
7楼-- · 2019-02-19 02:16

Get a list of all the option values and then check if the value contains your partial text. Something like this:

List<WebElement> list = driver.findElements(By.tagName("option"));
Iterator<WebElement> i = list.iterator();
while(i.hasNext()) {
    WebElement wel = i.next();    
    if(wel.getText().contains("your partial text"))
    {
       //select this option
    }
} 
查看更多
登录 后发表回答