Xpath to get text from xml

74 views
Skip to first unread message

norman bond

unread,
May 5, 2026, 7:45:09 AMMay 5
to Automate for Android
Hi, I'm probably making silly mistake but I cannot get his xpath 
to work in Inspect layout block.

xpathEncode(".//*[@android:id=\"@com.gotogames.funbridge:id/result_screen_ranking_header_rank\"]/@android:text")
There is only 1 such entry in the xml layout.
All I get back is the encoded xpath not the text on screen.
Result is set to string

Henrik "The Developer" Lindqvist

unread,
May 5, 2026, 8:28:43 AMMay 5
to Automate for Android
The xpathEncode function is for including text in a XPath predicate as a string, not for the encoding the the whole expression.
Try using just:
.//*[@android:id='@com.gotogames.funbridge:id/result_screen_ranking_header_rank']/@android:text

norman bond

unread,
May 6, 2026, 7:54:28 AMMay 6
to Automate for Android
Thanks.".//*[@android:id=\"@com.gotogames.funbridge:id/cell_result_screen_ranking_player_pseudo\"]/@android:text"
Sorry but a couple more questions.
The app I'm processing has display of one off info at the top followed by a s⁵crollable list of player info.

1) how can I get 2 or more attributes from 1 Inspect. I thought it was just by adding further clauses at the end,
 Eg "/@android:text/@android:layout_y" but returns null.

2) how to process the scroll able list. No predicate or [1] returns the 1st matching data, 
Eg ".//*[@android:id=\"@com.gotogames.funbridge:id/cell_result_screen_ranking_player_pseudo\"]/@android:text"
Or ".//*[@android:id=\"@com.gotogames.funbridge:id/cell_result_screen_ranking_player_pseudo\"][1]/@android:text"
But [2] etc returns null.

I do have a working flow (complex as does process other screens as well) using findall on the layout, but would like t
o use Inspect as a more robust solution

Henrik "The Developer" Lindqvist

unread,
May 6, 2026, 9:28:20 AMMay 6
to Automate for Android
  1. Try using an union expression, e.g. .//android.widget.TextView | .//android.widget.ImageView
  2. It should work, but keep in mind that it usually only include visible items, and that the [1] position predicate reflects the visible index which may not correspond to the actual list position, use the @android:layout_row for that.
    Maybe item [2] had not loaded, yet to be rendered, when the layout was inspected, try using a short Delay to have more items be loaded

norman bond

unread,
May 6, 2026, 11:10:32 AMMay 6
to Automate for Android
Hi.
1) not sure i follow that. I want 2 attributes from the same
 element. I've seen examples in other forums (for different tools)
 shown as /attr1/attr2 after the element path
2) definitely visible on screen, however there is no layout_row
 in the xml so I guess it won't  be possible.

Henrik "The Developer" Lindqvist

unread,
May 6, 2026, 1:52:17 PMMay 6
to Automate for Android
  1. e.g. .//android.widget.TextView/@*[name()='android:text' or name()='android:layout_y'], see here.

norman bond

unread,
May 7, 2026, 6:48:03 AMMay 7
to Automate for Android
Thanks,  but that selects an element with either of those 2 attributes. What I was
 looking for getting 2 (or more) attributes from a single element.
I've looked at several forums but none of the suggestions works for me.

Henrik "The Developer" Lindqvist

unread,
May 8, 2026, 12:21:35 PMMay 8
to Automate for Android
No, that should select all attributes /@* with those name()=, ensure Result type is Node-set.

norman bond

unread,
May 9, 2026, 5:19:43 AMMay 9
to Automate for Android
Thanks for all your suggestions but I'm giving this solution up
 as I have a working flow even if its not elegant.
I've tried 

.//android.widget.TextView/@*[name()='android:text' or name()='android:layout_y']"

As well as my own xpath, but it always fails with
com.llamalab.automate.RequiredVariableMissingException: variable

As an aside does automate support xpath functions such as ' contains'?

Henrik "The Developer" Lindqvist

unread,
May 9, 2026, 9:09:10 AMMay 9
to Automate for Android
The  com.llamalab.automate.RequiredVariableMissingException is likely due to your XPath including an {variable} somewhere, to avoid string interpolation, do \{variable}, see: https://llamalab.com/automate/doc/value.html#text
All XPath version 1.0 functions are supported, with some extra common ones, see: https://llamalab.com/automate/doc/appendix.html#xpath

Android 16 Baklava

unread,
10:29 AM (3 hours ago) 10:29 AM
to automa...@googlegroups.com
Ok

--
You received this message because you are subscribed to the Google Groups "Automate for Android" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automate-use...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/automate-user/c2423d94-31f1-4c28-b46d-e0a7f6275ecbn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages