Page 1 of 1

traversing the xpath with relationships

Posted: Mon Jul 02, 2012 7:20 am
by zpeach
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']

Re: traversing the xpath with relationships

Posted: Mon Jul 02, 2012 2:45 pm
by Support Team
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

Re: traversing the xpath with relationships

Posted: Mon Jul 02, 2012 2:56 pm
by omayer
-

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 ); 	
		
			
	
		}     

Re: traversing the xpath with relationships

Posted: Mon Jul 02, 2012 5:04 pm
by zpeach
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

Re: traversing the xpath with relationships

Posted: Tue Jul 03, 2012 10:22 am
by Support Team
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

Re: traversing the xpath with relationships

Posted: Tue Jul 03, 2012 10:59 am
by sdaly
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']");

Re: traversing the xpath with relationships

Posted: Wed Feb 26, 2014 3:29 pm
by mzperix
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