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

后端 未结 9 1778
温柔的废话
温柔的废话 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:36

    Using java 8 Awaitility

    Awaitility.await()
    .atMost(5, TimeUnit.SECONDS)
    .until(() -> select.getOptions().size() > 0);
    
    0 讨论(0)
  • 2020-12-14 18:42

    Its work for me:

      WebElement element = driver.findElement(By.xpath("//select[@name='NamE']/option[1]"));
      return element.getAttribute("text");
    
    0 讨论(0)
  • 2020-12-14 18:42

    Try the below code

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import TimeoutException
    
    try:
        # 10 is the maximum time to wait
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "#mySelectId option[value='valueofOptionYouExpectToBeLoaded']"))
        )
    except TimeoutException:
        print("Time out")
    
    #  Write your code
    
    0 讨论(0)
  • 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<WebDriver>(driver)
                        .withTimeout(60, TimeUnit.SECONDS)
                        .pollingEvery(10, TimeUnit.MILLISECONDS)
                        .until(new Predicate<WebDriver>() {
                            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.

    0 讨论(0)
  • 2020-12-14 18:49

    I used waitForElementPresent with a css target.

    Example: To wait for

    <select id="myselect"></select>
    

    to be populated with

    <option value="123">One-two-three</option>
    

    use

    • Command: waitForElementPresent
    • Target: css=#myselect option[value=123]
    • Value: (leave it empty)
    0 讨论(0)
  • 2020-12-14 18:50

    I think you should be use

    waitForElementPresent command. If possible let's me see your selenium IDE code.

    0 讨论(0)
提交回复
热议问题