Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath For Returning the Column Index

I have the following html:

<html>
  <head>
    <body>
      <table>
       <tr>
      <td>Something Else</td>
      <td>Something</td>
    </tr>
    <tr></tr>
  </table>
</body>

The xpath expression for the first row second column is:

/html/body/table/tbody/tr/td[2]

I'd like to use selenium to get the value '2'.

In other words, what is the value for the first occurrence of text "Something". I'm able to get selenium to recognize that element but not return the value.

Edit: So far some great answers. But let me clarify that this is more of a selenium question as opposed to a xpath question. I need to figure out how to get selenium to return this value.

like image 648
KevinO Avatar asked Dec 09 '11 16:12

KevinO


1 Answers

The following expression returns the position (column index) of the first cell containing the desired text:

count(/html/body/table/tr/td[.='Something'][1]/preceding-sibling::td) + 1
like image 67
Wayne Avatar answered Sep 21 '22 20:09

Wayne