TransWikia.com

findElement is not working in Appium on Android

Software Quality Assurance & Testing Asked on October 25, 2021

I am trying to click on display button in Settings app.
But “findElementBy..” statement is not working, Sometimes I get ‘nullpointer exception’ or ‘no such element exist’ exception and some times test passes without clicking Display button.

Click to View Image

I have tried with different application but still the findElementBy… is not working in Win10.
But the same piece of code is working, when I run it on the same real device from my Win 7 laptop.

Project is using following jars:

  1. gson-2.2.2.jar

  2. java-client-4.1.2.jar

  3. selenium-server-standalone-3.4.0.jar

  4. Android Development Tool for Eclipse (addon)

  5. Android for Maven Eclipse 1.4.0 (addon)

  6. Appium Sever (ver: 1.4.13.1)

Appium Code:

@Test
public void Script1() throws Exception{

    DesiredCapabilities capabilities = new DesiredCapabilities();

    capabilities.setCapability("automationName", "UiAutomator 2");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("platformVersion", "6.0.1");
    capabilities.setCapability("deviceName", "Redmi 3S");
    capabilities.setCapability("appPackage", "com.android.settings");
    capabilities.setCapability("appActivity", "com.android.settings.MainSettings");

    AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

    Thread.sleep(5000); 
driver.findElementByAndroidUIAutomator("UiSelector().text("Display")").click();
}

}

Appium Logs:

Launching Appium server with command: C:Program Files (x86)Appiumnode.exe libservermain.js –address 127.0.0.1 –port 4723 –platform-name Android –platform-version 23 –automation-name Appium –log-no-color

warn: Appium support for versions of node < 0.12 has been deprecated and will be removed in a future version. Please upgrade!

info: Welcome to Appium v1.4.13 (REV c75d8adcb66a75818a542fe1891a34260c21f76a)

info: Appium REST http interface listener started on 127.0.0.1:4723

info: [debug] Non-default server args: {“address”:”127.0.0.1″,”logNoColors”:true,”platformName”:”Android”,”platformVersion”:”23″,”automationName”:”Appium”}

info: Console LogLevel: debug

info: –> POST /wd/hub/session {“desiredCapabilities”:{“appPackage”:”com.android.settings”,”appActivity”:”com.android.settings.MainSettings”,”platformVersion”:”6.0.1″,”automationName”:”UiAutomator 2″,”platformName”:”Android”,”deviceName”:”Redmi 3S”},”requiredCapabilities”:{},”capabilities”:{“desiredCapabilities”:{“appPackage”:”com.android.settings”,”appActivity”:”com.android.settings.MainSettings”,”platformVersion”:”6.0.1″,”automationName”:”UiAutomator 2″,”platformName”:”Android”,”deviceName”:”Redmi 3S”},”requiredCapabilities”:{},”alwaysMatch”:{“platformName”:”Android”},”firstMatch”:[]}}

info: Client User-Agent string: Apache-HttpClient/4.5.3 (Java/1.8.0_144)

info: [debug] Didn’t get app but did get Android package, will attempt to launch it on the device

info: [debug] Creating new appium session 44f52f68-60a6-4469-9575-0c1bfe2d999d

info: Starting android appium

info: [debug] Getting Java version

info: Java version is: 1.8.0_144

info: [debug] Checking whether adb is present

info: [debug] Using adb from C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe

warn: No app capability, can’t parse package/activity

info: [debug] Using fast reset? true

info: [debug] Preparing device for session

info: [debug] Not checking whether app is present since we are assuming it’s already on the device

info: Retrieving device

info: [debug] Trying to find a connected android device

info: [debug] Getting connected devices…

info: [debug] executing cmd: “C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe” devices

info: [debug] 1 device(s) connected

info: Found device 964aa907d030

info: [debug] Setting device id to 964aa907d030

info: [debug] Waiting for device to be ready and to respond to shell commands (timeout = 5)

info: [debug] executing cmd: “C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe” -s 964aa907d030 wait-for-device

info: [debug] executing cmd: “C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe” -s 964aa907d030 shell “echo ‘ready'”

info: [debug] Starting logcat capture

info: [debug] Getting device API level

info: [debug] executing cmd: “C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe” -s 964aa907d030 shell “getprop ro.build.version.sdk”

info: [debug] Device is at API Level 23

info: Device API level is: 23

info: [debug] Extracting strings for language: default

info: [debug] Apk doesn’t exist locally

info: [debug] Could not get strings, but it looks like we had an old strings file anyway, so ignoring

info: [debug] executing cmd: “C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe” -s 964aa907d030 shell “rm -rf /data/local/tmp/strings.json”

info: [debug] Not uninstalling app since server not started with –full-reset

info: [debug] Skipping install since we launched with a package instead of an app path

info: [debug] Forwarding system:4724 to device:4724

info: [debug] executing cmd: “C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe” -s 964aa907d030 forward tcp:4724 tcp:4724

info: [debug] Pushing appium bootstrap to device…

info: [debug] executing cmd: “C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe” -s 964aa907d030 push “C:Program Files (x86)Appiumnode_modulesappiumbuildandroid_bootstrapAppiumBootstrap.jar” /data/local/tmp/

info: [debug] Pushing settings apk to device…

info: [debug] executing cmd: “C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe” -s 964aa907d030 install “C:Program Files (x86)Appiumnode_modulesappiumbuildsettings_apksettings_apk-debug.apk”

info: [debug] Pushing unlock helper app to device…

info: [debug] executing cmd: “C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe” -s 964aa907d030 install “C:Program Files (x86)Appiumnode_modulesappiumbuildunlock_apkunlock_apk-debug.apk”

info: Starting App

info: [debug] Attempting to kill all ‘uiautomator’ processes

info: [debug] Getting all processes with ‘uiautomator’

info: [debug] executing cmd: “C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe” -s 964aa907d030 shell “ps ‘uiautomator'”

info: [debug] No matching processes found

info: [debug] Running bootstrap

info: [debug] spawning: C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe -s 964aa907d030 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap -e pkg com.android.settings -e disableAndroidWatchers false

info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1

info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=

info: [debug] [UIAUTOMATOR STDOUT] io.appium.android.bootstrap.Bootstrap:

info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner

info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer

info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap

info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1

info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 1

info: [debug] [BOOTSTRAP] [debug] Socket opened on port 4724

info: [debug] [BOOTSTRAP] [debug] Appium Socket Server Ready

info: [debug] Waking up device if it’s not alive

info: [debug] Pushing command to appium work queue: [“wake”,{}]

info: [debug] [BOOTSTRAP] [debug] Loading json…

info: [debug] [BOOTSTRAP] [debug] Registered crash watchers.

info: [debug] [BOOTSTRAP] [debug] Client connected

info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:”action”,”action”:”wake”,”params”:{}}

info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION

info: [debug] [BOOTSTRAP] [debug] Got command action: wake

info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:0,”value”:true}

info: [debug] executing cmd: “C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe” -s 964aa907d030 shell “dumpsys window”

info: [debug] Screen already unlocked, continuing.

info: [debug] Pushing command to appium work queue: [“getDataDir”,{}]

info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:”action”,”action”:”getDataDir”,”params”:{}}

info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION

info: [debug] [BOOTSTRAP] [debug] Got command action: getDataDir

info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:0,”value”:”/data/local/tmp”}

info: [debug] dataDir set to: /data/local/tmp

info: [debug] Pushing command to appium work queue: [“compressedLayoutHierarchy”,{“compressLayout”:false}]

info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:”action”,”action”:”compressedLayoutHierarchy”,”params”:{“compressLayout”:false}}

info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION

info: [debug] [BOOTSTRAP] [debug] Got command action: compressedLayoutHierarchy

info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:0,”value”:false}

info: [debug] Getting device API level

info: [debug] executing cmd: “C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe” -s 964aa907d030 shell “getprop ro.build.version.sdk”

info: [debug] Device is at API Level 23

info: [debug] executing cmd: “C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe” -s 964aa907d030 shell “am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n com.android.settings/com.android.settings.MainSettings”

info: [debug] Waiting for pkg “com.android.settings” and activity “com.android.settings.MainSettings” to be focused

info: [debug] Getting focused package and activity

info: [debug] executing cmd: “C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe” -s 964aa907d030 shell “dumpsys window windows”

info: [debug] executing cmd: “C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe” -s 964aa907d030 shell “getprop ro.build.version.release”

info: [debug] Device is at release version 6.0.1

info: [debug] Device launched! Ready for commands

info: [debug] Setting command timeout to the default of 60 secs

info: [debug] Appium session started with sessionId 44f52f68-60a6-4469-9575-0c1bfe2d999d

info: <– POST /wd/hub/session 303 6622.421 ms – 74

info: –> GET /wd/hub/session/44f52f68-60a6-4469-9575-0c1bfe2d999d {}

info: [debug] Responding to client with success: {“status”:0,”value”:{“platform”:”LINUX”,”browserName”:”Android”,”platformVersion”:”6.0.1″,”webStorageEnabled”:false,”takesScreenshot”:true,”javascriptEnabled”:true,”databaseEnabled”:false,”networkConnectionEnabled”:true,”locationContextEnabled”:false,”warnings”:{},”desired”:{“appPackage”:”com.android.settings”,”appActivity”:”com.android.settings.MainSettings”,”platformVersion”:”6.0.1″,”automationName”:”UiAutomator 2″,”platformName”:”Android”,”deviceName”:”Redmi 3S”},”appPackage”:”com.android.settings”,”appActivity”:”com.android.settings.MainSettings”,”automationName”:”UiAutomator 2″,”platformName”:”Android”,”deviceName”:”964aa907d030″},”sessionId”:”44f52f68-60a6-4469-9575-0c1bfe2d999d”}

info: <– GET /wd/hub/session/44f52f68-60a6-4469-9575-0c1bfe2d999d 200 1.705 ms – 695 {“status”:0,”value”:{“platform”:”LINUX”,”browserName”:”Android”,”platformVersion”:”6.0.1″,”webStorageEnabled”:false,”takesScreenshot”:true,”javascriptEnabled”:true,”databaseEnabled”:false,”networkConnectionEnabled”:true,”locationContextEnabled”:false,”warnings”:{},”desired”:{“appPackage”:”com.android.settings”,”appActivity”:”com.android.settings.MainSettings”,”platformVersion”:”6.0.1″,”automationName”:”UiAutomator 2″,”platformName”:”Android”,”deviceName”:”Redmi 3S”},”appPackage”:”com.android.settings”,”appActivity”:”com.android.settings.MainSettings”,”automationName”:”UiAutomator 2″,”platformName”:”Android”,”deviceName”:”964aa907d030″},”sessionId”:”44f52f68-60a6-4469-9575-0c1bfe2d999d”}

info: –> POST /wd/hub/session/44f52f68-60a6-4469-9575-0c1bfe2d999d/element {“using”:”-android uiautomator”,”value”:”UiSelector().text(“Display”)”}

info: [debug] Waiting up to 0ms for condition

info: [debug] Pushing command to appium work queue: [“find”,{“strategy”:”-android uiautomator”,”selector”:”UiSelector().text(“Display”)”,”context”:””,”multiple”:false}]

info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:”action”,”action”:”find”,”params”:{“strategy”:”-android uiautomator”,”selector”:”UiSelector().text(“Display”)”,”context”:””,”multiple”:false}}

info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION

info: [debug] [BOOTSTRAP] [debug] Got command action: find

info: [debug] [BOOTSTRAP] [debug] Finding UiSelector().text(“Display”) using ANDROID_UIAUTOMATOR with the contextId: multiple: false

info: [debug] [BOOTSTRAP] [debug] Parsing selector: UiSelector().text(“Display”)

info: [debug] [BOOTSTRAP] [debug] UiSelector coerce type: class java.lang.String arg: “Display”

info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[TEXT=Display]

info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:0,”value”:{“ELEMENT”:”1″}}

info: [debug] Responding to client with success: {“status”:0,”value”:{“ELEMENT”:”1″},”sessionId”:”44f52f68-60a6-4469-9575-0c1bfe2d999d”}

info: <– POST /wd/hub/session/44f52f68-60a6-4469-9575-0c1bfe2d999d/element 200 82.340 ms – 87 {“status”:0,”value”:{“ELEMENT”:”1″},”sessionId”:”44f52f68-60a6-4469-9575-0c1bfe2d999d”}

info: –> POST /wd/hub/session/44f52f68-60a6-4469-9575-0c1bfe2d999d/element/1/click {“id”:”1″}
info: [debug] Pushing command to appium work queue: [“element:click”,{“elementId”:”1″}]

info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:”action”,”action”:”element:click”,”params”:{“elementId”:”1″}}

info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION

info: [debug] [BOOTSTRAP] [debug] Got command action: click

info: [debug] Responding to client with success: {“status”:0,”value”:true,”sessionId”:”44f52f68-60a6-4469-9575-0c1bfe2d999d”}

info: <– POST /wd/hub/session/44f52f68-60a6-4469-9575-0c1bfe2d999d/element/1/click 200 40.169 ms – 76 {“status”:0,”value”:true,”sessionId”:”44f52f68-60a6-4469-9575-0c1bfe2d999d”}

info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:0,”value”:true}

info: [debug] Didn’t get a new command in 60 secs, shutting down…

info: Shutting down appium session

info: [debug] Pressing the HOME button

info: [debug] executing cmd: “C:Program Files (x86)Androidandroid-sdkplatform-toolsadb.exe” -s 964aa907d030 shell “input keyevent 3”

info: [debug] Stopping logcat capture

info: [debug] Logcat terminated with code null, signal SIGTERM

info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:”shutdown”}

info: [debug] [BOOTSTRAP] [debug] Got command of type SHUTDOWN

info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:0,”value”:”OK, shutting down”}

info: [debug] Sent shutdown command, waiting for UiAutomator to stop…

info: [debug] [BOOTSTRAP] [debug] Closed client connection

info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1

info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=.

info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner

info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer

info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap

info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1

info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 0

info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=

info: [debug] [UIAUTOMATOR STDOUT] Test results for WatcherResultPrinter=.

info: [debug] [UIAUTOMATOR STDOUT] Time: 67.859

info: [debug] [UIAUTOMATOR STDOUT] OK (1 test)

info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: -1

info: [debug] UiAutomator shut down normally

info: [debug] Cleaning up android objects

info: [debug] Cleaning up appium session

info: [debug] We shut down because no new commands came in

3 Answers

The Issue was related to the JAR files compatibility among them. After using following versions jar viz

  1. gson-2.8.2.jar
  2. java-client-5.0.3.jar
  3. junit-4.12.jar
  4. selenium-server-standalone-3.5.3.jar

It worked fine with the current xpath as stated in the question.

Thanks for your participation.

Answered by Sandeep on October 25, 2021

In this case you can find those element by using xpath or name

driver.findElement(By.Name("Display")).click();

or

driver.findElement(new By.ByName("Display")).click();

or

driver.findElement(By.xpath("//android.widget.TextView[@text='Display']")).click();

Answered by Al Imran on October 25, 2021

Use xpath to find this element this worked for me every time

driver.findElement(By.xpath("//android.widget.TextView[@text='Display']").click();

Answered by PRasd on October 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP