Using Selenium 2's IWebDriver to interact with elements on the page
Asked Answered
W

2

6

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 ?

Whiteside answered 11/1, 2011 at 12:12 Comment(0)
A
8

As 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

<!DOCTYPE html>
<head>
<title>Disposable Page</title>
</head>
    <body >
        <select id="select">
          <option value="volvo">Volvo</option>
          <option value="saab">Saab</option>
          <option value="mercedes">Mercedes</option>
          <option value="audi">Audi</option>
        </select>
    </body>
</html>

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.

using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium;
using System.Collections.Generic;
using OpenQA.Selenium.IE;

namespace Selenium2
{
    class SelectExample
    {
        public static void Main(string[] args)
        {
            IWebDriver driver = new InternetExplorerDriver();
            driver.Navigate().GoToUrl("www.example.com");

            //note how here's i'm passing in a normal IWebElement to the Select
            // constructor
            Select select = new Select(driver.FindElement(By.Id("select")));
            IList<IWebElement> options = select.GetOptions();
            foreach (IWebElement option in options)
            {
                System.Console.WriteLine(option.Text);
            }
            select.SelectByValue("audi");

            //This is only here so you have time to read the output and 
            System.Console.ReadLine();
            driver.Quit();

        }
    }
}

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

driver.FindElements(By.TagName("select"));

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.

Anarchist answered 11/1, 2011 at 16:3 Comment(2)
+1 The Select class, as linked by ZloiAdun did the job, but I'm accepting this answer because you provided more information. Since the Select is not yet available in the public dll, I'm currently using the class and the exception linked by ZloiAdun. As regards the FindElements, I need to use that one because is more than 1 select that I want retrieved. Finally, thanks for mentioning the issue as regards DefaultSelenium, although I have a follow question to that; Is there any way to make use of the tons of methods that DefaultSelenium offers?Whiteside
+1 for clarifying IWebDriver being the way to go with 2.x. Just starting out and most of the demos I could find use DefaultSelenium.Merow
M
2

You should get all option elements from your select using ddl.FindElements(By.TagName("option"));. Then you can iterate through the returned collection and select required item(s) by using SetSelected method of the IWebElement

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 class

Marleen answered 11/1, 2011 at 13:48 Comment(2)
IWebElement does not have a method SetSelected. There is a method called Select but for my case, it's not doing anything.Whiteside
I am not using C#, so I can miss the method names - sorry for that. Have you tried using OpenQA.Selenium.Support.UI.Select class? Are you using the Select() on the option of the select?Marleen

© 2022 - 2024 — McMap. All rights reserved.