How to use WindowsDriver in Appium to wait for an element to load before clicking on it

I am using Appium with Windows Driver, to test my Windows based Dot net application, using C#… I am trying to click on a link through my automation tests. When my automation test is running, then its not waiting till my element(link) in the application is loaded/visible . Because of this my test fails. Is there a WinAppDriver /Windows Driver waittillElementisvisible property (like how we have in Selenium Webdriver) so that it waits till the element is loaded ? Any help is much appreciated.

1 Like

Any help with this one ?

You can use this:

public void WaitForElement(string IDType, string elementName, int time)
{
var wait = new DefaultWait<WindowsDriver>(driver)
{
Timeout = TimeSpan.FromSeconds(time),
PollingInterval = TimeSpan.FromSeconds(0.5)
};

        wait.IgnoreExceptionTypes(typeof(InvalidOperationException));

        wait.Until(driver =>
          {
              int elementCount = 0;
              switch (IDType)
              {
                  case "id":
                      elementCount = driver.FindElementsByAccessibilityId(elementName).Count;
                      break;
                  case "xpath":
                      elementCount = driver.FindElementsByXPath(elementName).Count;
                      break;
                  case "name":
                      elementCount = driver.FindElementsByName(elementName).Count;
                      break;
              }
              return elementCount > 0;
          });
    }
1 Like

@RaviChandraReddy Is there a way to update this to wait for an element to become enabled? Rather than just existing, if a button is not enabled immediately, can I make it so that it will wait for it to become enabled?

Hi RaviChandra,

To be sure I have understood your question, we are waiting on an element to appear. In other words, say we upload a file and it takes 30 seconds to a minute to finish uploading to display the element Upload Complete. This can be achieved in 2 ways.

Either way, ExpectedConditions.VisibilityOfAllElementsLocatedBy has helped. This waited for the number of minutes specified and completed the Appium Test successfully.

ExpectedConditions.ElementExists did not help. The Appium Test script did not wait even for a second and failed immediately.

Explicit Wait:
new WebDriverWait(driver, TimeSpan.FromMinutes(5)).Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath("//*[@nodeName='ION-CARD-HEAD")));

In the above Explicit Wait, the WebDriverWait by default checks for the element say Upload Complete which doesn’t appear immediately once in every 500 milliseconds. This is the functionality of Explicit Wait. However, I am setting a TimeSpan of 5 minutes. This means that the code will wait for 5 minutes and WebDriver wait’s functionality shall check for the “Upload Complete” element once in every 500 milliseconds.

Fluent Wait:
DefaultWait fluentWait = new DefaultWait(driver);
fluentWait.Timeout = TimeSpan.FromMinutes(5);
fluentWait.PollingInterval = TimeSpan.FromMilliseconds(1000);
/* Ignore the exception - NoSuchElementException that indicates that the element is not
present */
fluentWait.IgnoreExceptionTypes(typeof(NoSuchElementException));
fluentWait.Message = “Element to be searched not found”;

        fluentWait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath("//* 
        [@nodeName='ION-CARD-HEADER' and ./*[@name='cloud-done']]")));

In the above FluentWait, the timeout is set to 5 minutes alongside PollingInterval is set to 1000 milliseconds. This means that once in every 1000 milliseconds, the code will check if the “Upload Complete” text appears on the screen. If not it will keep trying until the timeout specified which on this occasion is 5 minutes.

I hope you find that this would solve your problem.

Regards,
K.Raghavan