[Newbie] Appium NoSuchElementException

Hello. I am new on appium and I’ve spotted problem I am unable to deal with.

I am practiing appium on ApiDemos-debug.apk delivered by Android developers I believe.

This is my code:

package tutorial;
import io.appium.java_client.android.*;
import java.net.MalformedURLException;
import java.util.concurrent.TimeUnit;
import io.appium.java_client.TouchAction;
public class gestures extends base {

	
	public static void main(String[] args) throws MalformedURLException, InterruptedException {
		
		AndroidDriver<AndroidElement> driver = Capabilities();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //manage waits? use INSTEAD of thread.sleep(SECONDS);
		
		//enter Views tab
		driver.findElementByXPath("//android.widget.TextView[@text='Views']").click();
		
		//Tap action. Create new class object and import appropriate library
		TouchAction touch = new TouchAction(driver);
		
		//actual tapping
		//alternatively: touch.tap(driver.findElementByXPath("(//android.widget.TextView)[10]")).perform();
		touch.tap(driver.findElementByXPath("//android.widget.TextView[@text='Expandable Lists']")).perform();
		touch.tap(driver.findElementByXPath("//android.widget.TextView[@text='1. Custom Adapter']")).perform();
}
}

Everything is ok to the almost line:

touch.tap(driver.findElementByXPath("//android.widget.TextView[@text='Expandable Lists']")).perform();

Program enters “Expandable Lists” but the next line fails:

touch.tap(driver.findElementByXPath("//android.widget.TextView[@text='1. Custom Adapter']")).perform();

Console log:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 11.54 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'DESKTOP-7Q1276D', ip: '172.16.5.147', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{app=C:\Users\Konrad\workspace\ApiDemos\src\tutorial\ApiDemos-debug.apk, appPackage=io.appium.android.apis, deviceScreenSize=1200x1920, networkConnectionEnabled=true, warnings={}, appWaitPackage=io.appium.android.apis, appWaitActivity=io.appium.android.apis.ApiDemos, databaseEnabled=false, deviceName=CB5A2370B9, platform=LINUX, deviceUDID=CB5A2370B9, appActivity=io.appium.android.apis.ApiDemos, desired={app=C:\Users\Konrad\workspace\ApiDemos\src\tutorial\ApiDemos-debug.apk, platformName=Android, deviceName=CB5A2370B9}, platformVersion=6.0.1, webStorageEnabled=false, locationContextEnabled=false, takesScreenshot=true, javascriptEnabled=true, deviceModel=SGP611, platformName=Android, deviceManufacturer=Sony}]
Session ID: 4da5db73-75b2-435b-a0fa-d16305b6e733
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
	at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
	at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
	at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
	at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
	at io.appium.java_client.PerformsTouchActions.performTouchAction(PerformsTouchActions.java:39)
	at io.appium.java_client.TouchAction.perform(TouchAction.java:381)
	at tutorial.gestures.main(gestures.java:26)

Why and how? These two lines are almost the same, only name is different… I would appreciate solution and/or some materials to read about this.

@edit
base class only sets up capabilities and driver.
Greetings.

Hey @darnok662, I’ve tried here roughly the same code, and instead of seeing NoSuchElementException I kept seeing StaleObjectException for the second tap action.

I’m using the ApiDemos (com.example.android.apis) that comes inside the Android Emulator, I had to adjust the DPI in order to find the Views option without scrolling.

In order to make it work I had to make some changes, mainly I had to create a new instance of TouchAction before using it. After that it ran many times in a row without any problem.

I add to this gist: https://gist.github.com/takakim/55d28dc61df7f7c4dcfa1fd050cc45c5

Try to give correct wait for particular element for “No such element exception”

Stale Element Exception - Check if element path is correct and It is available on DOM

Exception Details

https://seleniumhq.github.io/selenium/docs/api/py/common/selenium.common.exceptions.html

Hey.

Creating new instance of TouchAction helped! Thanks, but why it works? I’d like to know that.

Another thing is I got “unknown server-side error” on that command:

touch2.longPress(driver.findElementByAndroidUIAutomator(“text("People Names")”)).release().perform();

while depreciated version with actionWait(); works:

touch2.longPress(driver.findElementByAndroidUIAutomator(“text("People Names")”)).waitAction(2000).perform();

Appium logs:

[HTTP] ← POST /wd/hub/session/b4df4ecb-f03b-437b-b8e1-f38e2a45080a/element 200 687 ms - 87
[HTTP] → POST /wd/hub/session/b4df4ecb-f03b-437b-b8e1-f38e2a45080a/touch/perform {“actions”:[{“action”:“tap”,“options”:{“element”:“3”}},{“action”:“longPress”,“options”:{“element”:“4”}},{“action”:“release”,“options”:{}}]}
[debug] [MJSONWP] Calling AppiumDriver.performTouch() with args: [[{“action”:“tap”,“options”:{“element”:“3”}},{“action”:“longPress”,“options”:{“element”:“4”}},{“action”:“release”,“options”:{}}],“b4df4ecb-f03b-437b-b8e1-f38e2a45080a”]
[debug] [AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“element:getLocation”,“params”:{“elementId”:“3”}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“element:getLocation”,“params”:{“elementId”:“3”}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: getLocation
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:0,“value”:{“x”:0,“y”:176}}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“element:getSize”,“params”:{“elementId”:“3”}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“element:getSize”,“params”:{“elementId”:“3”}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: getSize
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:0,“value”:{“width”:1200,“height”:64}}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“element:getLocation”,“params”:{“elementId”:“4”}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“element:getLocation”,“params”:{“elementId”:“4”}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: getLocation
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:0,“value”:{“x”:0,“y”:176}}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“element:getSize”,“params”:{“elementId”:“4”}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“element:getSize”,“params”:{“elementId”:“4”}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: getSize
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:0,“value”:{“width”:1200,“height”:64}}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“element:click”,“params”:{“elementId”:“3”,“x”:600,“y”:208}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“element:click”,“params”:{“elementId”:“3”,“x”:600,“y”:208}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: click
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:0,“value”:true}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“element:touchLongClick”,“params”:{“elementId”:“4”,“x”:600,“y”:208,“duration”:1000}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“element:touchLongClick”,“params”:{“elementId”:“4”,“x”:600,“y”:208,“duration”:1000}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: touchLongClick
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Performing TouchLongClick using element? true x: 600, y: 208, duration: 1000
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:0,“value”:true}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“element:touchUp”,“params”:{“x”:600,“y”:208}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“element:touchUp”,“params”:{“x”:600,“y”:208}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: touchUp
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Display bounds: [0,0][1200,1824]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Performing TouchUp using element? false x: 600, y: 208
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:13,“value”:“Failed to execute touch event”}
[debug] [AndroidBootstrap] Received command result from bootstrap
[HTTP] ← POST /wd/hub/session/b4df4ecb-f03b-437b-b8e1-f38e2a45080a/touch/perform 500 1928 ms - 154

Hey @darnok662 can you check the version of appium and java-client you’re using? I’m currently using 1.6.5 and 5.0.0-BETA9.
Both options worked fine, I think the reason you always need to create a new instance of the TouchAction it was design to work like that.
Check the TouchActions wiki from the java-client github
I also update the gist code including the longPress actions.

Hey @Massanori_Takaki

I’m using exactly the same versions as You. I did not have time for this over weekend, but today I will try updating/replacing my libs. I do not understand the last part:

I also update the gist code including the longPress actions.

Could You specify/explain?

Thanks in advance.

Greetings, darnok662

Hey @darnok662

I also update the gist code including the longPress actions.

I’ve previously add a example code, which I updated including the longPress action, on the gist link sent before, you can find the code I’m using here.

Hey @Massanori_Takaki.

Before I had overlooked you code but i just tried it out and it works perfectly fine and gave me much insight into the code. Thank You for your effort!

No problem, glad to help.