question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Explicit Wait not working in Appium 1.8.0 with c# client

See original GitHub issue

Description

Explicit wait not working and the sever doesn’t wait for the time specified for the object to be present

Environment

Appium version 1.8.0 Desktop OS/version used to run Appium: Windows 10 Pro Node.js version (unless using Appium.app|exe): 8.10.0 Mobile platform/version under test: Android API 25 ,v7.1.1 Real device or emulator/simulator: Real Device Samsung Tab Appium CLI or Appium.app|exe: both IDE: VisualStudio 2017 Enterprise and C# as coding language

Code To Reproduce Issue [ Good To Have ]

new WebDriverWait(driver, TimeSpan.FromMilliseconds(50000)).Until(ExpectedConditions.ElementToBeClickable(By.ClassName(“android.widget.Spinner”)));

and also tried this code

new WebDriverWait(driver,FromMilliseconds(50000)).Until(X=>X.FindElement(By.Id(“elementid”);

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(50)); Func<IWebDriver, IWebElement> waitForElement = new Func<IWebDriver, IWebElement>((IWebDriver Web) => {

             IWebElement element = Web.FindElement(By.XPath("//android.widget.Button[@text='OK']"));
             if (element.GetAttribute("style").Contains("red"))
             {
                 return element;
             }
             return null;
         });

IWebElement ok =wait.Until(waitForElement);

Ecxeption stacktraces

Test Name: TestMethod1 Test FullName: UnitTestProject1.UnitTest1.TestMethod1 Test Source: C:\Users\gokulnath.kumar\source\repos\UnitTestProject1\UnitTestProject1\UnitTest1.cs : line 25 Test Outcome: Failed Test Duration: 0:01:49.3963396

Result StackTrace:

at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) at OpenQA.Selenium.Appium.AppiumDriver1.Execute(String driverCommandToExecute, Dictionary2 parameters) at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value) at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByXPath(String xpath) at OpenQA.Selenium.By.<>c__DisplayClass19_0.<XPath>b__0(ISearchContext context) at OpenQA.Selenium.By.FindElement(ISearchContext context) at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by) at OpenQA.Selenium.Appium.AppiumDriver1.FindElement(By by) at AMCS_Mobile.Page_Objects.Vehicle_Selector_Objects.OK_btn() in C:\Users\gokulnath.kumar\source\repos\UnitTestProject1\UnitTestProject1\Page_Objects\Vehicle_Selector_Objects.cs:line 36 at AMCS_Mobile.Vehicle_Selector.Vehicle_selector.Vehicle_Selector(String Vehiclename) in C:\Users\gokulnath.kumar\source\repos\UnitTestProject1\UnitTestProject1\App_Modules\Vehicle_selector.cs:line 30 at UnitTestProject1.UnitTest1.TestMethod1() in C:\Users\gokulnath.kumar\source\repos\UnitTestProject1\UnitTestProject1\UnitTest1.cs:line 29 Result Message: Test method UnitTestProject1.UnitTest1.TestMethod1 threw exception: System.InvalidOperationException: An element could not be located on the page using the given search parameters.

Link to Appium logs

https://gist.github.com/Gocoollnath/547096033a7ff6a9bc4c89b9f30f7d1b

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
EbenZhangcommented, Apr 20, 2021

I did a bit research of this issue, but not totally sure about the second point

  • Quite obvious that the ImplicitWait time has to be smaller than the one you passed to WebDriverWait. So you need to set it to a small amount and set back to the original value after the WebDriverWait.
  • The WebDriverWait is expecting a NotFoundException when element not found. But seems appnium (maybe not from appnium) is throwing WebDriverException instead.

I guess below would workaround the issue

WebDriverWait GetWebDriverWait(IWebDriver driver, TimeSpan waitTime)
{
   var wait = new WebDriverWait(driver, waitTime);
   wait.IgnoreExceptionTypes(typeof(NotFoundException),
        typeof(InvalidOperationException), // i guess some version may throw this exception instead.
        typeof(WebDriverException));
   return wait;
}

UseTheWebDriverWait() {
  using(Disposable(onInit: SetImplicitWaitTimeToSmallerAmount, onDispose: SetImplicitWaitTimeBack)){
     var wait = GetWebDriverWait(theDriver, theTimeToWait);
     wait.Until(......);
  }
}
0reactions
Dor-blcommented, Nov 13, 2022

Closing this issue since it’s the expected behavior. As @EbenZhang mentioned, you should use: wait.IgnoreExceptionTypes(typeof(NotFoundException) . In addition, you should wrap your code with try-catch, in case element is not found in the given time frame.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Explicit Wait not working in Appium 1.8.0 with c# client #225
Description Explicit wait not working and the sever doesn't wait for the time specified for the object to be present Environment Appium ......
Read more >
Explicit wait not working while using MobileElement
I am using appium to automate a android mobile application. I am finding elements using accessibility id. When the app launches, i want...
Read more >
Implicit & Explicit wait is not reliable to find the element
Hi, I am running the appium test for hybrid app on iOS. I am using the wait condition until the element is clickable.But...
Read more >
Part 5 | Don't use Explicit Waits | Wait Plugin in Appium 2.0
Part 5 | Don't use Explicit Waits | Wait Plugin in Appium 2.0 | Server Side Waits ... and Checklist | Latest Tutorials...
Read more >
@appium/test-support | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found