2

I am trying to figure out how to know which is the position of current node under it parent, currently I am using selenium and most of the time I use Xpath. This is because I want to extract all the columns of a table In this example the number of columns are dynamically generated

<tabe>
    <tr>
        <td>23</td>
        <td>42</td>
        <td>31</td>
    </tr>
    <tr>
        <td>13</td>
        <td>21</td>
        <td>32</td>
    </tr>
</table>

So lets say that I need the second colunm (42 and 21), but since the the content is dynamically generated I don't know if it is the second colum I only know that I want the colum with the number 42. So with selenium I get the 42 easily. Bu I need the position of it td.

I have thought in an algorithm that might be complex and slow. Basically something like:

  1. Get the html code under current
  2. Get the parent of current node
  3. Iterate (while counting) got an the parent and compare each child inner code with the one that I found.
  4. Some other validations that I need to think
  5. When I found it that's the colum

That algorithm have some issues like "What if there are 2 elements with the same inner code" and as I mentioned that might be slow and don't let me start when I start considering the Row and colum span (currently I just need the position in the current row).

So there is a easier way to do this? Some selenium function that I don't know or maybe a way to get it though XPath?

Thanks :)

Yoiku
  • 882
  • 2
  • 13
  • 25
  • What should happen if there are "2 elements with the same inner code" ? – zx485 Feb 05 '16 at 17:21
  • With the current algorithm that I have I will find the first one. But in that case I might blame the user for trying to search an element with the same characteristics as other. I know is not the best way but currently I don't see any other solution. That's why I think it would be better if I can get the position just having the IWebElement. Maybe if there is a way to compare 2 selenium IWebElements that could fix that first issue – Yoiku Feb 05 '16 at 17:26
  • may I first ask "why"? I'm genuinely curious what you will do with the position after you have it – ddavison Feb 05 '16 at 17:45
  • Yes, It is for an automation testing project. So we need to have the possibility validate if all the elements of a column have the correct value. For example lets say that we need to compare the values of "year" column with the expected values that we have in an excel. The only thing we know is the ID of the element year column, we don't know the column number since some other columns might appear in the middle depending on certain conditions that we ignore at run time – Yoiku Feb 05 '16 at 17:58

1 Answers1

1

Playing around a little with XPath i came up with the following query;

//tr[2]/td[position()=count(//tr[1]/td[.='42']/preceding-sibling::*) + 1]

This will give you the relative positioned "TD" of the 2nd "TR" from a selection of text found in the "TD" of the 1st "TR".

This was based on Find position of a node using xpath

Community
  • 1
  • 1
Marvin Smit
  • 4,088
  • 1
  • 22
  • 21
  • Thank you! position()=count(//tr[1]/td[.='42']/preceding-sibling::*) + 1 this was what I was lookin for, and the source where you extract the data was really helpful :) Thanks!!! – Yoiku Feb 08 '16 at 22:00