Search code examples
testingwebdriverselenium-webdriverfunctional-testing

WebDriver SelectElement get selected value


var test = SelectElement([TheIWebElemement]);
Assert.AreEqual("55", test.SelectedOption.Text);

Based on the above snippet, does anyone know how to get the selected value in the dropdown. I am opening a form in edit mode, so I know the value I expect the selected option to have. I do not want the text, I want the value behind the option as the text I don't care about. From my inspecting, all I can seem to get at is the text of the option.


Solution

  • The SelectedOption that is returned is simply the IWebElement representing that option.

    Therefore, your question becomes 'how do I get the value of an option element?'. Since the SelectElement is kind and has given you the IWebElement for free, you can simply do:

    var selectedValue = test.SelectedOption.GetAttribute("value");
    Assert.IsNotNullOrEmpty(selectedValue);
    Assert.AreEqual("the value", selectedValue);