I'm using Selenium's IWebDriver
to write Unit Tests in C#.
Such is an example:
IWebDriver defaultDriver = new InternetExplorerDriver();
var ddl = driver.FindElements(By.TagName("select"));
The last line retrieves the select
HTML element wrapped in a IWebElement
.
I need a way to simulate selection to a specific option
in that select
list but I can't figure out how to do it.
Upon some research, I found examples where people are using the ISelenium DefaultSelenium
class to accomplish the following, but I am not making use of this class because I'm doing everything with IWebDriver
and INavigation
(from defaultDriver.Navigate()
).
I also noticed that ISelenium DefaultSelenium
contains a ton of other methods that aren't available in the concrete implementations of IWebDriver
.
So is there any way I can use IWebDriver
and INavigation
in conjunction with ISelenium DefaultSelenium
?
You should get all
option
elements from yourselect
usingddl.FindElements(By.TagName("option"));
. Then you can iterate through the returned collection and select required item(s) by usingSetSelected
method of theIWebElement
Update: It seems that there's now a C# implementation of the
Select
in WebDriver - previously it was in Java only. Please take a look at its code and it is easier to use this classAs ZloiAdun mentions, there is a lovely new Select class in the OpenQA.Selenium.Support.UI namespace. That's one of the best ways to access a selection element and it's options because it's api is so easy. Let's say you've got a web page that looks something like this
You're code to access the select would look like this. Note how I create the Select object by passing a normal IWebElement to it's constructor. You have plenty of methods on the Select object. Take a look at the source for more information, until it gets properly documented.
A couple things to note about the Support class however. Even if you downloaded the latest beta, the support DLL won't be there. The Support package has a relatively long history in the Java libraries (that's where PageObject lives) but it's still pretty fresh in the .Net driver. Fortunately, it's really easy to build from source. I pulled from SVN then referenced the WebDriver.Common.dll from the beta download and built in C# Express 2008. This class hasn't been as well tested as some of the other classes, but my example worked in Internet Explorer and Firefox.
There's a few other things that I should point out based on your code above. Firstly the line you were using to find the select element
is going to find all select elements. you should probably use
driver.FindElement
, without the 's'.Also, very rarely would you use INavigation directly. You'll do most of your navigation like
driver.Navigate().GoToUrl("http://example.com");
Lastly,
DefaultSelenium
is the way to access the older Selenium 1.x apis. Selenium 2 is a pretty significant departure from Selenium 1, so unless you're trying to migrate old tests to the new Selenium 2 api (often referred to as the WebDriver api) you won't use DefaultSelenium.