TransWikia.com

How to wait until dropdown selected option changes using selenium?

Stack Overflow Asked by rajesh paripelly on December 18, 2021

Sometimes, based on some other actions on page, a dropdown selected item changes automatically without page reload (this dropdown has same no. of options everytime).
How can we wait until dropdown selected item changes using selenium webdriver C#?

<select name="ddlFruit_N" id="ddlFruit_N" class="Searchddl" style="text-decoration: none; width: 152px; height: 22px;">
    <option value="">Select</option>
    <option value="447">Grapes</option>
    <option value="448">Mango</option>
    <option selected="selected" value="449">Apple</option>
</select>

One Answer

Please try below code to wait for changed dropdown option.

public static SelectElement FindSelectElementWhenPopulated(IWebDriver driver, By by, int delayInSeconds, string optionText)
    {
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(delayInSeconds));
        return wait.Until<SelectElement>(drv => 
        {
            SelectElement element = new SelectElement(drv.FindElement(by));
            if (element.SelectedOption.ToString().Contains(optionText))
            {
                return element;
            }

            return null;
        }
        );
    }

and I call it like this to wait for changed drop down option “Orange”

Myclass.FindSelectElementWhenPopulated(driver, By.CssSelector("#ddlFruit_N"), 20, "Orange");

Answered by Muzzamil on December 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP