traversing the xpath with relationships

Ranorex Spy, Recorder, and Studio.
zpeach
Posts: 2
Joined: Tue Jun 05, 2012 9:57 pm

traversing the xpath with relationships

Post by zpeach » Mon Jul 02, 2012 7:20 am

I need some help traversing the xpath using relationships without using the repository. Please provide example code that will select the image based on the cousin's (sibling, uncle, step-sister, mother's brother's girlfriend, ..joking.. etc) innertext

How do I do the following using code? My goal is to be able to select and click on the imgage via xpath based on the sibling (or whatever relationship) related inner text entry. In other words,
I don't want to use the repository and just use xp path to do the following. What is the c# code syntax need to select the image object based on the inner text?
I must do this for several different inner texts.

Image Object Example A:
/dom[@domain='10.2.xx.20']//table[#'isc_B1table']/tbody/tr[2]/td[2]/tag/img[@src~'^http://10\.2\.xx\.20/avct/im']
Inner Text Example A:
/dom[@domain='10.2.xx.20']//table[#'isc_B1table']/tbody/tr[2]/td[1]/tag[1]/table/tbody/tr/td[3]/tag[@innertext='Left Wing']

Image Object Example B:
/dom[@domain='10.2.xx.20']//table[#'isc_B1table']/tbody/tr[3]/td[2]/tag/img[@src~'^http://10\.2\.xx\.20/avct/im']
Inner Text Example B:
/dom[@domain='10.2.xx.20']//table[#'isc_B1table']/tbody/tr[3]/td[1]/tag[1]/table/tbody/tr/td[3]/tag[@innertext='Right Wing']

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: traversing the xpath with relationships

Post by Support Team » Mon Jul 02, 2012 2:45 pm

Hi,

I am not sure I completely got the question.
Do you mean something like that?
//Image Object Example A:
       ImgTag img = "/dom[@domain='10.2.xx.20']//table[#'isc_B1table']/tbody/tr[2]/td[2]/tag/img[@src~'^http://10.2.xx.20/avct/im']";
       img.Click();
       // Inner Text Example A:
       WebElement webEl = "/dom[@domain='10.2.xx.20']//table[#'isc_B1table']/tbody/tr[2]/td[1]/tag[1]/table/tbody/tr/td[3]/tag[@innertext='Left Wing']";
       webEl.Click();
You have to create a specific adapter before you can use its methods, Ranorex UI Adapter
There are also samples about how to work with UserCode: Code Examples.

Regards,
Markus
Ranorex Support Team
.
Image

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: traversing the xpath with relationships

Post by omayer » Mon Jul 02, 2012 2:56 pm

-

Code: Select all

public string InnerTextA()
    
        {
    
   WebElement elem = "/dom[@page='index.cfm']/body/table[1]/tbody/tr[2]/td[2]/table/tbody/tr[12]/td/b";
        string _OrderID = elem.InnerText;
Report.Success( _OrderID ); 	
		
			
	
		}     
Tipu

zpeach
Posts: 2
Joined: Tue Jun 05, 2012 9:57 pm

Re: traversing the xpath with relationships

Post by zpeach » Mon Jul 02, 2012 5:04 pm

Here is what I am trying to do, find the following from path B (tr[2]/td[1]/tag[1]/table/tbody/tr/td[3]/tag[@innertext='Left Wing']) and use it to locate the image in path A because the two have this in common (/dom[@domain='10.2.xx.20']//table[#'isc_B1table']/tbody/tr[2]/)

Seems to me with one of Ranorex's xpath relationships (like preceding-sibling or something similar) I should ba able to do this.

Path A
"/dom[@domain='10.2.xx.20']//table[#'isc_B1table']/tbody/tr[2]/td[2]/tag/img[@src~'^http://10.2.xx.20/avct/im']"

Path B
"/dom[@domain='10.2.xx.20']//table[#'isc_B1table']/tbody/tr[2]/td[1]/tag[1]/table/tbody/tr/td[3]/tag[@innertext='Left Wing']";

Thanks,

Zachary

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: traversing the xpath with relationships

Post by Support Team » Tue Jul 03, 2012 10:22 am

Hi,

Do you mean you want to concatenate them?
String pathA = "/dom[@domain='10.2.xx.20']//table[#'isc_B1table']/tbody/tr[2]/";
String pathB = "td[1]/tag[1]/table/tbody/tr/td[3]/tag[@innertext='Left Wing']";

WebElement el = pathA+pathB;
Regards,
Markus
Ranorex Support Team
.
Image

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Re: traversing the xpath with relationships

Post by sdaly » Tue Jul 03, 2012 10:59 am

You mean like this?

WebDocument dom = "/dom[@domain='10.2.xx.20']";

TrTag tableRow = dom.FindSingle(".//table[#'isc_B1table']/tbody/tr[2]");

ImgTag imgA = tableRow.FindSingle("./td[2]/tag/img[@src~'^http://10.2.xx.20/avct/im']");
ImgTag imgB = tableRow.FindSingle("./td[1]/tag[1]/table/tbody/tr/td[3]/tag[@innertext='Left Wing']");

mzperix
Posts: 137
Joined: Fri Apr 06, 2012 12:19 pm

Re: traversing the xpath with relationships

Post by mzperix » Wed Feb 26, 2014 3:29 pm

So, you have the following scenario:
1. look for a specific text in a table
2. in the row, where the text is, click on the image

Am I getting right the idea?

If this is the problem, than the image's rsanorexpath would be the following (I changed the color to red, where I made changes):

/dom[@domain='10.2.xx.20']//table[#'isc_B1table']/tbody//tr/td[1]/tag[1]/table/tbody/tr/td[3]/tag[@innertext='This is the text you look for']/../../../../../../../td[2]/tag/img[@src~'^http://10.2.xx.20/avct/im']

As you can see, I combined the two xpath in a tricky way :)
First you find the row you look for. That's why you need to change /tr[2] to //tr, so Ranorex will traverse every row, until it finds the first text match.
Then you go up on the path one parent with every /../ part until you get to the proper tr element. After it, you just go down on the proper td element to get to your image.

Also, you can use a variable instead of the hard coded string.

Regards,
Zoltan