1

HTML:

<select name="ddlFruit" id="ddlFruit" class="Searchddl">
    <option value="">Select</option>
    <option value="447">Grapes</option>
    <option value="448">Mango</option>
    <option selected="selected" value="449">Apple</option>
</select>

Suppose "Apple" is in first selected mode, due to some other actions on site, this drop-down changes to other options automatically. I want webdriver to wait until "Mango" text is in selected mode.

Tried code:

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;
        }
        );
    }

Myclass.FindSelectElementWhenPopulated(driver, By.CssSelector("#ddlFruit"), 20, "Mango");

I am using C#.

by dukaan
  • 17
  • 6
  • Show us your current code and error traces – JaSON Jul 24 '20 at 11:39
  • Updated plz check @JaSON – by dukaan Jul 24 '20 at 11:45
  • You can try to wait until element `//option[@selected="selected" and .="Mango"]` found – JaSON Jul 24 '20 at 11:52
  • can you try this? `wait.until(ExpectedCondition.elementSelectionStateToBe(element, true));` reference - https://stackoverflow.com/questions/35208638/difference-between-expectedconditions-elementtobeselected-and-elementselectionst – Raj Jul 24 '20 at 11:53

2 Answers2

1

You don't want to convert the SelectedOption to a string. Test the Text property instead:

if (element.SelectedOption.Text.Contains(optionText))

With a few changes, you can make this a handy extension method on WebDriverWait:

public static SelectElement UntilOptionIsSelected(this WebDriverWait wait, By by, string optionText)
{
    return wait.Until<SelectElement>(driver => 
    {
        var element = new SelectElement(driver.FindElement(by));

        if (element.SelectedOption.Text.Contains(optionText))
        {
            return element;
        }

        return null;
    });
}

And to use it:

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
var dropdown = wait.UntilOptionIsSelected(By.CssSelector("#ddlFruit"), "Mango");
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
  • Working: if (element.SelectedOption.Text.Contains(optionText)) – by dukaan Jul 24 '20 at 12:08
  • @bydukaan: I'm not sure what you mean by your last comment. – Greg Burghardt Jul 24 '20 at 12:10
  • Hi @Greg Bughardt , plz help me by solving this issue [link](https://stackoverflow.com/questions/63084609/how-to-wait-until-javascript-fully-loaded-in-a-div-using-selenium-webdriver) – by dukaan Jul 25 '20 at 05:14
  • Hi there, a similar type of problem. Plz help [link](https://stackoverflow.com/questions/63104447/how-to-wait-until-div-dropdown-is-loaded-using-selenium-webdriver) @Greg Bughardt – by dukaan Jul 26 '20 at 21:12
  • Hi there, Could you please answer this question [Link](https://stackoverflow.com/q/65999006/12826684). Thank you @greg-burghardt – by dukaan Feb 01 '21 at 19:37
  • Hi there, Could you please solve this issue.[link](https://stackoverflow.com/q/65999006/12826684) Thank you @Greg Burghardt – by dukaan Feb 01 '21 at 19:44
-3

You need to use the Select class in Selenium:

Select dropdowns=new Select(driver.findelement(By.id("ddlFruit")));
dropdowns.selectByVisibleText("Mango");
 

    
d219
  • 2,707
  • 5
  • 31
  • 36
Justin Lambert
  • 940
  • 1
  • 7
  • 13