33

(Migrating from Java-Selenium to C#-Selenium)

When searching for explicit waits with Selenium and C# I find several posts with code that looks similar to the Java-Counterpart:

for example here:

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0,0,5));
wait.Until(By.Id("login"));

or here:

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("locator")));

WebDriverWait are from the OpenQA.Selenium.Support.UI namespace and comes in a separate package called Selenium WebDriver Support Classes

BUT:
When I try to code this myself in Visual Studio (getting Selenium package via NuGet), using WebDriverWait in my code will prompt the message:

The type or namespace name 'WebDriverWait' could not be found (are you missing a using directive or an assembly reference?)

Even though I am including the Selenium reference via

using OpenQA.Selenium;

When looking up the documentation for WebDriverWait you will find, that there should be a namespace called

OpenQA.Selenium.Support.UI

But I cannot access it via "using" in my code.

Why does this happen? Where can I find the WebDriverWait class?

Community
  • 1
  • 1
drkthng
  • 6,651
  • 7
  • 33
  • 53

4 Answers4

64

Luckily I sometimes read the comments to answers as well, so I stumbled across the solution within the highest ranked comment here:

WebDriverWait [is] from the OpenQA.Selenium.Support.UI namespace and comes in a separate package called Selenium WebDriver Support Classes on NuGet

Thanks @Ved!

In Visual Studio this means, that you need to install TWO packages:

  • NuGet package "Selenium.WebDriver" AND ALSO
  • NuGet package "Selenium.Support"

Coming from Java with Maven, this is not trivial (at least to me ;-), because until now I just needed to include one and only one dependency to get "all of the good stuff" like this:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.46.0</version>
</dependency>

* Posted this as a question including the answer because it cost me too much time and luck to stumble over the answer.

Community
  • 1
  • 1
drkthng
  • 6,651
  • 7
  • 33
  • 53
  • 1
    Did you download the C#/.Net package on the Selenium site? There should be 4 DLLs in there. If you add a reference to all 4 of them you should be good. That's all I do and it works for me. I never use NuGet. – JeffC Sep 18 '15 at 13:17
  • @JeffC no, I used NuGet - thx for the hint, will update my question! – drkthng Sep 18 '15 at 13:20
  • You should accept your own answer, since you know it works. Makes it clearer to those reading this question trying to find a solution for themselves. – Shotgun Ninja Sep 18 '15 at 15:08
  • I can only accept it in 2 days from now ;-) but anyway thx for the support – drkthng Sep 18 '15 at 15:09
  • Hi ! I have both packages ( Selenium Support 3.141.0 and Selenium.WebDriver) and still can't use WebDriverWait. Any ideas? – Daniel Sep 21 '19 at 20:03
  • finally, i was struggling with `OpenQA*` imports for hours – Jack Aug 27 '22 at 04:50
3

Have you tried adding?

using OpenQA.Selenium.Support.UI;

That should be all you need. Depending on your IDE, it likely (?) will have a helper that will find and add missing references. I use Visual Studio. I just type what I know should be there. When the IDE puts red squiggles on WebDriverWait, I hover over it and it shows a tooltip that says I'm missing a reference and I click on the link, Show potential fixes. In the new tooltip it shows several options, usually the first of which is to add the using statement. I click that one and the red squiggles go away.

I really like Visual Studio but I've been using Eclipse for the last 9 months or so and I prefer the way Eclipse handles this. I think it's more intuitive... but that's just me.


EDIT

Just wanted to make clear for future readers... I don't use NuGet. I just download the Selenium .Net package and drop the 4 DLLs in/near my project and then add references to all 4 of them to my Visual Studio project. Once I do that and use the instructions above, it works for me.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • no this does not work, when you use NuGet -> you search for Selenium and add the first package that comes up, which is Selenium.WebDriver, and this does not include the Support classes, that's why as in my answer, you need to load another NuGet Packag called "Selenium Support" – drkthng Sep 18 '15 at 14:53
  • I was off adding the extra instructions for the way I install the Selenium .Net package since I don't use NuGet from our conversation below for clarity... – JeffC Sep 18 '15 at 14:56
  • yeah, without NuGet I think one is more used to add "several" references, but using NuGet, as is the same with Maven, you usually just add ONE dependency/package which is very convenient. So convenient and used to that I couldn't find the solution why I cannot access WebDriverWait ;-) – drkthng Sep 18 '15 at 14:58
  • 1
    Yeah it seems odd that there isn't just one NuGet package that contains all 4 DLLs. It's only like 4MB for all 4. – JeffC Sep 18 '15 at 15:00
  • good to hear that I'm not alone finding this odd -> I guess since we're two now, the question and answer is justified ;-) – drkthng Sep 18 '15 at 15:07
  • There are good and sufficient reasons why the packages are designed the way they are. The first is that originally, the .NET implementations of the support classes were supposed to be example code, not necessarily used in production. Nobody cared about that intent, so the support assembly was released as an artifact in its own right. Furthermore, when the NuGet packages were first implemented, guidance was that each package should contain a single referenced assembly. If this guidance has changed, the Selenium project hasn't changed accordingly. – JimEvans Sep 19 '15 at 12:04
1

WebDriverWait is not available in OpenQA.selenium. Its Available SeleniumExtras.WaitHelpers

    using OpenQA.Selenium;
    using SeleniumExtras.WaitHelpers;
        
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
  wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.Id("Test")));
AmitKS
  • 132
  • 4
-1

Selenium.WebDriver.WaitExtentions.

  • IWebDriver driver = new ChromeDriver(); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSecound(1)); driver.FindElement(By.Id("Login")); – Youngro Kim Feb 01 '18 at 03:17