Selenium Select - Selecting dropdown option by part of the text

前端 未结 8 1906
清歌不尽
清歌不尽 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"
                    }
                }
    

提交回复
热议问题