I have this select on a page:
<select multiple="" class="recipientsList" name="Recipients[]" id="To" style="display: none;">
<option value="User-6" class="">Coordinator, Test</option>
<option value="Course-4" class="">New Course 1</option>
<option value="UserType-6" class="">Coordinators</option>
<option value="UserTypeInCourse-4-6" class="">New Course 1 Coordinator</option>
</select>
And I’m running this test:
public IWebDriver WebDriver
{
get
{
// gets the current WebDriver instance, set up elsewhere at the beginning
// of the fixture
return ScenarioContext.Current.WebDriver();
}
}
public void SelectTest()
{
// code to navigate to proper page
var options = WebDriver.FindElements(By.CssSelector("select.recipientsList option"));
Assert.That(options, Is.Not.Empty, "No options found.");
Assert.That(!options.Any(option => string.IsNullOrEmpty(option.Text)), "Some or all options have blank text.");
// Actual useful assert
}
The second assert is failing because all of the elements in the options collection has empty string as their Text objects. It works if I remove the JavaScript on the page that adds the display:none; style. This is not a permanent solution though, as this select needs to be hidden, as it is extended by FCBKcomplete.
How do I get the text of hidden select options with Selenium 2/WebDriver in .NET?
WebDriver is designed to emulate the real user interactions. If something is not visible, then the real user cannot see it and WebDriver cannot see it either.
You can emulate the user actions – click, hover or whatever makes your select visible – and then find your select’s options and inspect them.