Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebDriverWait or ImplicitlyWait or ExplictlyWait nothing works

I'm using Selenium 2 tests (written in C#) that choose values from a "select" control. Selection causes a post-back to the server, which updates the state of the page. I am therefore performing a manual wait (thread.sleep) after choosing a value to wait for the page to be changed. and it works fine with Thread.Sleep. However, Thread.Sleep is a bad idea to use with number of good reasons so when I take out all my Thread.Sleep line of code then all my test cases fall apart and I have tried WebDriverWait, Implicitly and Explicitly none works and very frustration

below is the sample code that I have tried....

//WebDriverWait

 public IWebElement WaitForElement(By by)
 {
            // Tell webdriver to wait
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
            wait.PollingInterval = TimeSpan.FromSeconds(2);
            wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(NoSuchFrameException));
            wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException), typeof(StaleElementReferenceException));

            IWebElement myWait = wait.Until(x => x.FindElement(by));
            return myWait;
}

Tried this too:

   WebDriverWait wait = new WebDriverWait(new SystemClock(), driver, TimeSpan.FromSeconds(30), TimeSpan.FromMilliseconds(100));

//Implicitly:

driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));

//Explicit Wait:

IWebDriver driver = new FirefoxDriver();
driver.Url = "http://somedomain/url_that_delays_loading";
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
    {
        return d.FindElement(By.Id("someDynamicElement"));
    });
like image 519
Nick Kahn Avatar asked Oct 19 '12 03:10

Nick Kahn


People also ask

What is the use of WebDriverWait?

In automation testing, wait commands direct test execution to pause for a certain length of time before moving onto the next step. This enables WebDriver to check if one or more web elements are present/visible/enriched/clickable, etc.

What does WebDriverWait return?

By default, WebDriverWait calls the ExpectedCondition every 500 milliseconds until it returns success. ExpectedCondition will return true (Boolean) in case of success or not null if it fails to locate an element. There are some common conditions that are frequently of use when automating web browsers.

Is WebDriverWait a class or interface?

Both are classes and implements Wait interface. WebDriverWait class is an extension of FluentWait class. It doesn't have its own methods. Explicit wait waits for a certain condition till specific element is not loaded.

Is WebDriverWait a class?

A specialization of FluentWait that uses WebDriver instances.


1 Answers

Here is what works for me ->

WebDriverWait wait = new WebDriverWait(browser, new TimeSpan(0, 0, 30));

element = wait.Until<IWebElement>((driver) =>
  {
     return driver.FindElement(By.Name("name_of_element")));
  });

You can also do by ID ->

WebDriverWait wait = new WebDriverWait(browser, new TimeSpan(0, 0, 30));

element = wait.Until<IWebElement>((driver) =>
  {
     return driver.FindElement(By.Id("id_of_element")));
  });

Without seeing more of your code, it will be hard to determine why it's not working.

like image 98
hacket Avatar answered Nov 08 '22 08:11

hacket