How to find an element only by automation id/text in C#?

Ask general questions here.
Mariam637
Posts: 1
Joined: Fri Jul 19, 2019 8:43 am

How to find an element only by automation id/text in C#?

Post by Mariam637 » Mon Aug 05, 2019 6:40 am

I'm new to Ranorex and trying to use Ranorex's C# API for a Windows desktop application.
How to find an element only by automation id/text in C#?

Looking for something like,

Code: Select all

mainWindow.Get(SearchCriteria.ByAutomationId("<automationId>"))
mainWindow.Get<Button>("<automationId>");
or

Code: Select all

mainWindow.Button.GetElement("<automationId>");
But in the API docs, all I can see is XPath (RanoreXPath) based object identification.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to find an element only by automation id/text in C#?

Post by odklizec » Mon Aug 05, 2019 2:00 pm

Hi,

Ranorex is an xpath-based TA tool and therefore, its API heavily rely on xpaths. To find an element using automationid attribute, you can use something like this...

Code: Select all

Host.Local.FindSingle("//*[@automationid='idstring']");
or in variabilized form...

Code: Select all

Host.Local.FindSingle("//*[@automationid='" + varId + "']");
I would personally recommend using Ranorex repository and search the elements via code only if there is absolutely no way to use repo. And even then, you should use repo elements as starting points and search elements from given repo element e.g. like this:

Code: Select all

public static void SearchAnElement(RepoItemInfo repoElementLBL)
{
    if (repoElementLBL.Exists(10000))
    {
        var foundElement = repoElementLBL.CreateAdapter<Ranorex.Unknown>(false).FindSingle("//*[@automationid='idstring'");
        ...
    }    
}
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

Chartlon
Posts: 3
Joined: Sun Dec 06, 2020 10:08 am
Location: https://gbapps.net/gbwhatsapp-apk/
Contact:

Re: How to find an element only by automation id/text in C#?

Post by Chartlon » Sat Feb 13, 2021 11:15 am

reku wrote:
Mon Aug 12, 2019 11:44 am
Mariam637 wrote:
Mon Aug 05, 2019 6:40 am
I'm new to Ranorex and trying to use Ranorex's C# API for a Windows desktop application.
How to find an element only by automation id/text in C#?

Looking for something like, ogwhatsapp apk

Code: Select all

mainWindow.Get(SearchCriteria.ByAutomationId("<automationId>"))
mainWindow.Get<Button>("<automationId>");
or

Code: Select all

mainWindow.Button.GetElement("<automationId>");
But in the API docs, all I can see is XPath (RanoreXPath) based object identification.
Hi buddy, did you try the loop way? That should help I guess.
thank you so much for this, I was into this issue and tired to thinker around to check if it's possible but could it done. now that I have seen the way you did it, thanks again.

EthanWinters
Posts: 1
Joined: Sun Jul 18, 2021 11:00 am

Re: How to find an element only by automation id/text in C#?

Post by EthanWinters » Tue Sep 14, 2021 6:31 am

I am having the same issue in September 2021. And the code you have provided here, is not working.
It's not working sir, whenever I use this code, It shows an error. Can you please tell me another way to find an element only by automation id/text in C#?
Regards: Ethan
KineMaster Mods

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to find an element only by automation id/text in C#?

Post by odklizec » Tue Sep 14, 2021 7:00 am

Hi,

I'm afraid, I can't provide any further help without more details from your side.
What version of Ranorex are you using (most recent is 10.1.1.).
What kind of error exactly are you getting?
And finally, please post a Ranorex snapshot (NOT screenshot) of the problematic element you are trying to find.
THanks.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

peter009
Posts: 1
Joined: Tue Nov 30, 2021 5:42 am
Location: Delhi

Re: How to find an element only by automation id/text in C#?

Post by peter009 » Thu Feb 17, 2022 11:54 am

EthanWinters wrote:
Tue Sep 14, 2021 6:31 am
I am having the same issue in September 2021. And the code you have provided here, is not working.
It's not working sir, whenever I use this code, It shows an error. Can you please tell me another way to find an element only by automation id/text in C#?
Regards: Ethan
Check now in 2022. Its working right now.

Flokky
Posts: 2
Joined: Sat Aug 06, 2022 6:29 am

Re: How to find an element only by automation id/text in C#?

Post by Flokky » Sat Aug 06, 2022 7:03 am

peter009 wrote:
Thu Feb 17, 2022 11:54 am
Check now in 2022. Its working right now. WhatsApp Aero APK
The code worked for me too.