4

I'm using Selenium to target certain elements on a page.

I have a table that looks like this

<table>
    <tbody>
         <tr>
             <td>Text1</td>
             <td>Text2</td>
         </tr>
         ...
    </tbody>
</table>

Each <tr> element matches this pattern. I want to pass in two strings, text1 & text2, and find the row element whose two cells match text1 and text2, in that order. I'm assuming XPath is the way to go, I'm just not really sure how to write this case, if it's possible.

allie
  • 369
  • 1
  • 15
  • 1
    Have you tried anything? If no, why not? If yes, where are you stuck? – Tomalak Mar 10 '16 at 14:31
  • I dont know how to write the part that says 'parent tr such that it has a child td where the text = text1 and another child td where text = text2', mostly the part where i say it has to have both td's – allie Mar 10 '16 at 14:34
  • 1
    Okay. So how to write a single condition, say, *"Select the `` where the text is 'Text1'"*? Next, how about *"Select the `` where the text is 'Text1'" and the `string-length()` of the text is 5"*? Finally: Conditions can be nested. This should address all your issues. – Tomalak Mar 10 '16 at 14:39

2 Answers2

6

predicates can be combined:

//tr[td[1]="Text1" and td[2]="Text2"]
tdrury
  • 2,307
  • 1
  • 22
  • 25
1

I'm not sure as I can't check it , but try this xpath:

//td[.=Text1]/following-sibling::td[.=Text2]/parent::tr
Andersson
  • 51,635
  • 17
  • 77
  • 129