Appium Native app throws - Returned value cannot be converted to WebElement: {stacktrace=NoSuchElementError: An element could not be located on the page using the given search parameters

Hi,

while trying to automate native app for android, I’m facing the following issue,

Exception in thread “main” org.openqa.selenium.WebDriverException: Returned value cannot be converted to WebElement: {stacktrace=NoSuchElementError: An element could not be located on the page using the given search parameters.
at AndroidDriver.callee$0$0$ (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-android-driver/lib/commands/find.js:69:13)
at tryCatch (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-android-driver/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-android-driver/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-android-driver/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/Applications/Appium.app/Contents/Resources/app/node_modules/appium-android-driver/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at , error=no such element, message=An element could not be located on the page using the given search parameters.}
Build info: version: ‘3.5.2’, revision: ‘10229a9’, time: ‘2017-08-21T17:29:55.15Z’
System info: host: ‘USHYDJKUMARR3MB’, ip: ‘fe80:0:0:0:104a:38f2:7a2a:37ee%en0’, os.name: ‘Mac OS X’, os.arch: ‘x86_64’, os.version: ‘10.12.6’, java.version: ‘1.8.0_171’
Driver info: driver.version: AndroidDriver
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:421)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:62)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.j

Versions that I’had used,

Selenium - 3.11.0
appium java client - 5.0.0
guava - 22.0
appium version - 1.8.0
android studio - 3.1.2

please find the following code,
private static AndroidDriver driver;
public static void main(String[] args) throws MalformedURLException, InterruptedException {

        DesiredCapabilities capabilities = new DesiredCapabilities();
	capabilities.setCapability("deviceName", "Galaxy S8+");
	capabilities.setCapability("platformVersion", "7.0");
	capabilities.setCapability("platformName", "Android");
	capabilities.setCapability("app", "/Users/username/Desktop/APK/test.apk");
            capabilities.setCapability("appPackage", "com.xxx.android.xxxx.xxx");
            capabilities.setCapability("appActivity", "com.xxx.android.xxx.ui.login.LoginActivity");

            driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
	driver.findElement(By.id("et_member_number")).clear();
	driver.findElement(By.id("et_member_number")).sendKeys("6008");
	System.out.println(driver.getPageSource()); // able to get the page source with xml content 
	driver.findElementById("btn_next").click();
	System.out.println(driver.getPageSource()); // able to get the page source with xml content
	MobileElement Edit_Password = driver.findElementById("et_password");
	Thread.sleep(2000);
	Edit_Password.sendKeys("favourite user 123");
	MobileElement btn_Password = driver.findElementById("btn_login");
	btn_Password.click();
	Thread.sleep(2000);
	MobileElement lnk_Skip = driver.findElementById("skip");
	lnk_Skip.click();
	Thread.sleep(2000);
	driver.findElementById("button1").click();; // able to get the page source with xml content
	Thread.sleep(2000);

          **// able to perform action on above elements expect this**
	driver.findElementById("btn_start").click();  // **unable to get the page source** 

}}

The following things have been tried but none of them seems to working,

  1. Getpagesource() not returning any xml content for the btn_start
  2. appium desktop doesn’t load the screen for the btn_start (but for all other screens it gets loaded)
  3. Tried with other java client versions as well but no luck
  4. getcontext returns NATIVE and I see there is no web component in it but still unable to interact with these

Can I get any help on this …

Your error is telling you that the element could not be found on the page. I think this happens because your app hasn’t been installed and run yet.
I suggest you to try adding “appPackage”, “appWaitPackage” (or “appWaitActivity”) and “appActivity” to your desired capabilities.

Check the link below:
http://appium.io/docs/en/writing-running-appium/caps/#android-only

…let me know about the result.

Hey,

I’ve tried earlier and didn’t work … the issue is not even with wait time as well… Im trying to execute each line after the activity is loaded in debugging mode… thx

Is this "btn_start"on a previous Activity (which you hit after a successful login)?

thanks for the reply,

Yes, the btn_start is the activity screen that comes after clicking login button, skip button and a OK popup button.
and also the reason I’ve added “autowebview” is because the error talks about webelement hence it is added…

Ok I see what’s going on. I have the same issue here:

I suggest you put you application to Background/foreground before you call:

driver.findElementById(“btn_start”).click();

…by doing so:

(AndroidDriver) driver).runAppInBackground(Duration.ofMillis(1500));

Also, please let me know about the result.
P.S. i believe “autowebview” isn’t needed.

It worked with start button by adding this line,

driver.runAppInBackground(Duration.ofMillis(1500));

but after performing above action it launches a new screen with a button “btn_settings” again this is failing…I see same error that I had posted,

“Returned value cannot be converted to WebElement: {stacktrace=NoSuchElementError: An element could not be located on the page using the given search parameters.”

so, tried to get the pagesource after performing backgroundapprunning like this,

driver.runAppInBackground(Duration.ofMillis(1500));
System.out.println(driver.getPageSource()); // able to get the page source
driver.findElementById(“btn_start”).click(); // this is working now
driver.runAppInBackground(Duration.ofMillis(1500));
System.out.println(driver.getPageSource()); // unable to get the page source
driver.findElementById(“btn_settings”).click(); // unable to click on this element

How can I fix this now ?
also just wanted to know is there a way that I can ask my development team to avoid this issue or any other permanent solution rather doing (app running in background) for every element ?
please do the needful.

I think the autowebView is fouling you up. If after clicking one button, you’re brought to a page that contains a webview, appium (I believe) will switch into that webview (using chromedriver). That would explain why the getPageSource() before didn’t work (it doesn’t work correctly on a webview in my experience). Double check your context (and what context is available) in between each of these. I’d almost recommend turning off autowebView to prove something like this.

Hey… bennid,

It’s a complete native app, I’ve removed webview and it is not working… driver.context gives NATIVE

What if you do:

appDriver.getContextHandles()

A pure native app doesn’t mean there aren’t webviews in there (even if getContext() is returning NATIVE). In android apps as well, if the developer doesn’t enable webview debugging you won’t even see the webviews with appium (or chrome). Double-check with the dev (unless of course you’re the dev, then you know even better!) that there isn’t something hidden that you can’t see. Also, when you say “it is not working” after removing the autoWebview capability, what do you mean? Where is it failing?

It’s a Compatibility issue. Upgrade Appium Version