Get all elements using xpath

Ask general questions here.
satishmohan
Posts: 56
Joined: Wed Jun 10, 2015 4:35 pm

Get all elements using xpath

Post by satishmohan » Sun Apr 02, 2017 5:38 pm

Hi. I have a situation where i have to capture all the elements in a webpage based on the xpath i provide. Example- we have 10 rows and 10 columns. Column 5 of each row has the price listed. I want to capture all the prices in a list based on the xpath. In selenium it is easy since as below. Do we have a similar method as "findElements" based on xpath:

List<WebElement> grossPrice = driver.findElements(By.xpath("//td[@class='sorting_1']"));.

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

Re: Get all elements using xpath

Post by odklizec » Sun Apr 02, 2017 8:18 pm

Hi,

Please post a Ranorex snapshot of the element in question so we can better understand your GUI and provide reasonable suggestion. Without snapshot, it's much harder to give correct answer.
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

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Get all elements using xpath

Post by Support Team » Sun Apr 02, 2017 9:24 pm

Just for your info: Even without seeing a snapshot, you can search for all elements matching a RanoreXPath using the Find method, e.g.:
IList<WebElement> webElements = searchRoot.Find<WebElement>(".//td[@class='sorting_1']");
// or if you want elements of type TdTag
IList<TdTag> tdTags= searchRoot.Find<TdTag>(".//td[@class='sorting_1']");
Using the TdTag type provides a few more properties on each element than using the generic WebElement type (see the API doc).
Just make sure to use a relative path (precede it with a dot ".") to search from the "searchRoot" element, which can be the DOM or any web element underneath it.

Regards,
Alex
Ranorex Team

satishmohan
Posts: 56
Joined: Wed Jun 10, 2015 4:35 pm

Re: Get all elements using xpath

Post by satishmohan » Tue Apr 04, 2017 11:54 am

Hi Team. I could not get which element should be specified under "searchRoot". I am attaching a snapshot for ref. basically i am looking at getting all the values under Power column. Highlighted value of 9.8 is for First row alone. A reputation of multiple rows with the same structure (as of row-1) is continued for other values of Power.
You do not have the required permissions to view the files attached to this post.

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

Re: Get all elements using xpath

Post by odklizec » Tue Apr 04, 2017 12:53 pm

Hi,

What you have posted is unfortunately not snapshot, but screenshot, which is unfortunately not helpful. Please learn how to create one here...
http://www.ranorex.com/support/user-gui ... files.html
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

satishmohan
Posts: 56
Joined: Wed Jun 10, 2015 4:35 pm

Re: Get all elements using xpath

Post by satishmohan » Tue Apr 04, 2017 2:51 pm

Get that. Please find attached
You do not have the required permissions to view the files attached to this post.

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

Re: Get all elements using xpath

Post by odklizec » Tue Apr 04, 2017 3:40 pm

Thanks for the snapshot. OK, because there is no unique Id for individual cells belonging to the Capacity column (in your previous screenshot called 'Power'), there is no way to get all elements belonging to this column directly via a simple xpath.

What you need to do is to get the "ChildIndex" value for column 'Capacity' and store it in a variable. You can do this via GetValue recording action/method, referring to this xpath:

Code: Select all

/dom[@domain='sv42080oel8128']//table[#'resultList']/thead/tr/td[@innertext='Capacity [kW]']
Then you need to create a new repo item (let's call it ListofTDTags) where the $childIndexVar is the variable obtained via GetValue action...

Code: Select all

/dom[@domain='sv42080oel8128']//table[#'resultList']/tbody/tr/td[@childindex=$childIndexVar] 
This repo item returns all TD tags under "Capacity [kW]" column.

Now you can create iList of TdTag adapters from this repo item...

Code: Select all

IList<TdTag> tdList =   
    repo.ListofTDTagsInfo.CreateAdapters<TdTag>(); 
Hope this helps?

BTW, some time ago, I've posted a suggestion for a new HTML table relationship operators, allowing us to get cells, based of row/column relationship. This would solve our problem with HTML tables. Please vote for it here...
http://uservoice.ranorex.com/forums/150 ... -html-cell
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

satishmohan
Posts: 56
Joined: Wed Jun 10, 2015 4:35 pm

Re: Get all elements using xpath

Post by satishmohan » Wed Apr 05, 2017 3:30 pm

Perfect. Worked exactly the way you suggested. Thank you :)