Search code examples
c#seleniumwebdriver

Selenium WebDriver object


I have build a few tests using Selenium with FireFox. For this I used the FirefoxDriver:

private FirefoxDriver driver = new FirefoxDriver():

This works great, but now I want to be able to switch browsers, based on the users choice. I had hoped to use WebDriver as the type and then use a switch to determine which browser to use.

Unfortunately WebDriver gives the error: "Can not resolve symbol "WebDriver". Pretty much every example I find uses a WebDriver type specification though.

I have a reference to the WebDriver.dll in my project. And Chrome, Firefox and IE drivers are recognised. Just the generic driver is not. Can anyone tell me what I can do to make this work?


Solution

  • The various drivers all implement an interface called IWebDriver. However, you are using methods such as FindElementByClassName ...which is on the RemoteWebDriver. Although I'd advise you to drop this and use the basic .FindElement and pass in the type of selector (By) that you need, there is a way around it:

    You will need a using for OpenQA.Selenium.Remote

    private RemoteWebDriver driver;
    

    You can then do:

    driver = new FirefoxDriver();
    

    or...

    driver = new InternetExplorerDriver();
    

    You will still have access to your underlying FindElementBy... methods.

    I will also say you do not need the ThoughtWorks or WebDriverBackedSelenium libraries at all. Remove them.