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

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

    I found handy something like:

    wait.until(
        ExpectedConditions
            .presenceOfNestedElementsLocatedBy(By.id(<selectioNId>), By.tagName("option"))
    )
    
    0 讨论(0)
  • 2020-12-14 19:00

    You can use "WaitForSelectOption" command where your value can be direct label like label=1-saving Account target will have the object id

    0 讨论(0)
  • 2020-12-14 19:00

    I made the following function in C# that returns the select when is populated.

    You have to pass a By to find the element and you a specific time to wait for it to be filled:

    public static SelectElement FindSelectElementWhenPopulated(this IWebDriver driver, By by, int delayInSeconds)
        {
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(delayInSeconds));
            return wait.Until<SelectElement>(drv => 
            {
                SelectElement element = new SelectElement(drv.FindElement(by));
                if (element.Options.Count >= 2)
                {
                    return element;
                }
    
                return null;
            }
            );
        }
    

    In my case I validate tha the select has more than 2 options, you can change the code so that it validates the quantity that fits your needs.

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