Selenium: How to wait for options in a select to be populated?

后端 未结 9 1790
温柔的废话
温柔的废话 2020-12-14 18:14

I am using Selenium for the first time and am overwhelmed by the options. I am using the IDE in Firefox.

When my page loads, it subsequently fetches values via an JS

9条回答
  •  庸人自扰
    2020-12-14 18:43

    We had similar problem where the options within drop down was fetched from a third party service, which sometimes was a slower operation.

    Select select = new Select(driver.findElement(cssSelector("cssSelectorOfSelectBox")));    
    waitUnitlSelectOptionsPopulated(select)
    

    here is the definition for waitUnitlSelectOptionsPopulated

    private void waitUntilSelectOptionsPopulated(final Select select) {
                new FluentWait(driver)
                        .withTimeout(60, TimeUnit.SECONDS)
                        .pollingEvery(10, TimeUnit.MILLISECONDS)
                        .until(new Predicate() {
                            public boolean apply(WebDriver d) {
                                return (select.getOptions().size() > 1);
                            }
                        });
            }
    

    Note: A check of select.getOptions().size() >1 was needed in our case as we had one static option always displayed.

提交回复
热议问题