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: 6529
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

reku
Posts: 2
Joined: Mon Aug 12, 2019 11:33 am

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

Post by reku » 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,

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.

Chartlon
Posts: 4
Joined: Sun Dec 06, 2020 10:08 am

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.