1

I am completely new to Selenium, started using it yesterday and I am pretty much done with the first part of my project and I really love the way its going.

Altho I am having one problem, currently I am using Thread.Sleep for pauses until elements get present. There are some animations going on or just slow loading at times, how can I make it wait until the element is present and then interact with it?

For example:

        LoginPageElements loginPage = new LoginPageElements();
        loginPage.continueLink.Click();
        Thread.Sleep(5000); //here it has to wait until the next page loads

        ClickDailyBonusPopUp();

        Driver.driver.Navigate().GoToUrl(.....);
        Thread.Sleep(2000); //here it has to wait until a login form pops up

        LoginFormElements loginForm = new LoginFormElements();
        loginForm.userPasswordLogin.Click();
        Thread.Sleep(2000); //here it has to wait until a different login form pops up
Darkbound
  • 3,026
  • 7
  • 34
  • 72

2 Answers2

0

You need to use WebDriverWait class. Here is really good solution for your problem.

Community
  • 1
  • 1
Marcin
  • 773
  • 6
  • 17
  • Thanks a lot, but this solution did not work for me, because I am not accessing the elements with FindElement. I have separate files for the separate parts of the website with the different elements. As you can see on my first row of code I am initializing my login page elements, and all of the elements are then accessible from loginPage. If I use the method that you suggested, the constructor requires me to specify a By and I do not have that. For example, continue initialization: `[FindsBy(How = How.LinkText, Using = @"Продължи")]` `public IWebElement continueLink { get; set; }` – Darkbound Jun 13 '15 at 15:43
  • Nevermind, I used another solution from the same thread and it worked! For future reference to anyone struggling with this problem, the solution I used is: `driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));` From Mike Kwan. "An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance." – Darkbound Jun 13 '15 at 15:52
  • But please note that when you are navigating to new page you can wait until page is loaded by trying to select some element on it. You can select whatever you want, until it will inform you that page is loaded, it could be header text, logo, or other element, or `continueLink` (as in your example), then when you can select it, you will be sure that it is visible and you can click on it. – Marcin Jun 13 '15 at 15:54
0

If your application uses jQuery, I would make use of Selenium's IJavaScriptExecutor to do something like this:

public void WaitForAjax()
{
  if ((bool)((IJavaScriptExecutor)WebDriver).("return window.jQuery != undefined"))
  {
    while (true)
    {
      var ajaxIsComplete = (bool)((IJavaScriptExecutor)WebDriver).ExecuteScript("return jQuery.active == 0");
      if (ajaxIsComplete)
        break;
      Thread.Sleep(100);
    }
  }
}

(More on jQuery.active here.)

The main idea is to find some condition or element to poll for/wait on that is always present when the page isn't ready.

Community
  • 1
  • 1
misterjake
  • 159
  • 5