Selenium Select - Selecting dropdown option by part of the text

前端 未结 8 1860
清歌不尽
清歌不尽 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:21

    This will work

    WebElement element = driver.findEle(By.xpath(loc));
                Select select = new Select(element);
                for(int i=1;i<=select.getOptions().size();i++)
                {
                    if(select.getOptions().get(i-1).getText().contains(containsTxt))
                    {
                        select.selectByIndex(i-1);
                        break;
                    }
                    if(i==select.getOptions().size())
                    {   
                        //"Fail"
                    }
                }
    
    0 讨论(0)
  • 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")));
        }
    
    0 讨论(0)
  • 2020-12-19 17:24

    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)
    
    0 讨论(0)
  • 2020-12-19 17:26

    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<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.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)
      }
    
    0 讨论(0)
  • 2020-12-19 17:26

    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
        }
    } 
    
    0 讨论(0)
  • 2020-12-19 17:29

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题