Partial id search does not work

I tried only on android, version 9, in the emulator.

The following search works, and the id is clearly present in the appium-desktop session window.
await driver.$(‘id=android:id/button1’);

But it does not work when I try to only find a part, containing button1. Either adb says it cannot compile, or it says element not found.

await driver.$('//*[contains(@id, "button1")]');
await driver.$('//[contains(@id, "button1")]');
await driver.$('//[contains(@id, \'button1\')]');
await driver.$('//*[contains(@id, \'button1\')]');

By the way, webdriverio detects that it is an xpath if the locator starts with a slash.

I checked the doc // takes the active element. SO maybe just a slash should be used? WHy use one slash or 2?

I never could use xpath. Only //* works. The long xpath given in appium desktop also works.
Here it is:
/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.support.v7.widget.LinearLayoutCompat/android.widget.ScrollView/android.widget.LinearLayout/android.widget.Button

I also added a await sleep before to be sure the button is visible before the search is done. But the long xpath works so it should not be a problem of non present element.

Anyone knows? Thanks!

I also copy all the data on it in the inspector:

Find By Selector
id android:id/button1
xpath /hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.support.v7.widget.LinearLayoutCompat/android.widget.ScrollView/android.widget.LinearLayout/android.widget.Button
Attribute Value
elementId c1e5de65-e694-4db5-bd0a-9cefceb039b3
index 0
package com.generosales.striker
class android.widget.Button
text OK
resource-id android:id/button1
checkable false
checked false
clickable true
enabled true
focusable true
focused false
long-clickable false
password false
scrollable false
selected false
bounds [777,940][969,1084]
displayed true

This xpath worked:
//*[contains(@resource-id,‘button1’)]’

But I have a problem now because this is android-specific. I thought the id in an xpath could be multi-platform like for the exact match when using id=.

But wit the exact match, I must use an android specific id.

Hence I will use 2 different selector for 2 different platforms, using an if.

//*[contains(@resource-id,‘button1’) or contains(@id,‘button1’)] could be a workaround

Yes a union of queries looks good. I will test on iOS later when my tests are ready. I have many things to fix for iOS.

Why do you use backticks in the quotes? Backticks do not compile in adb.

I will use an if else because an exact match is less likely to collide than a contains.

if (process.env.PLATFORM === 'android') {
  okButton = await driver.$('id=android:id/button1');
} else {
  okButton = await driver.$('id=button1');
}

I don’t. It’s the forum engine, that changes it automatically.

try better with UIselector for android:

 @AndroidFindBy(uiAutomator = "new UiSelector().resourceIdMatches(\".*button1\")")