I need to verify that a <tr>
exists which contains 3 <td>
with specific values.
There are 100 <tr>
tags on the page. I am trying to filter them using 1 xpath.
for instance if I filter all the <tr>
on these 3 values:
//td[contains(text(),'Belgium')]
//td[contains(text(),'be-stelara-cd-uc')]
//td[contains(text(),'2022-07')]
That should return 1 <tr>
containing those <td>
.
So what I need to do, would be to filter each xpath against the previous leaving a single unique remaining. I came across the |
union operator which is almost what I want but instead of combining the results I want to exclude them.
I tried this xpath:
//tr[contains(td/text(), 'Belgium') and contains(td/text(), 'be-stelara-cd-uc') and contains(td/text(), '2022-07')]
html example:
Edit:
//tr[contains(td/text(), 'Belgium') and contains(td[2]/text(), 'be-stelara-cd-uc') and contains(td[3]/text(), '2022-07')]
This works, I did not realise that the td would check for ordering as well. If possible I would like to tweak the question to come up with a universal solution considering that the order cannot be guaranteed.
CodePudding user response:
If you can rely on the data in the picture, that is: no whitespace and no other inline-elements more than shown, you could try this:
//tr[td[text()= 'Belgium'] and td[text()= 'be-stelara-cd-uc'] and td[contains(text(), '2022-07')]]
If you want to be extra sure and want to filter unwanted space you could try this:
//tr[td[text()[normalize-space()= 'Belgium']] and td[text()[normalize-space()= 'be-stelara-cd-uc']] and td[contains(text(), '2022-07')]]
If more content can be present just use the .
and contains()
together like this:
//tr[td[contains(., 'Belgium')]and td[contains(., 'be-stelara-cd-uc')]and td[contains(., '2022-07')]]
And to be complete you could also just test the text-content of the tr like this:
//tr[contains(., 'Belgium') and contains(., 'be-stelara-cd-uc') and contains(., '2022-07')]
CodePudding user response:
Use any of the below xpath
to identify the tr
//tr[.//td[contains(., 'Belgium')]][.//td[contains(., 'be-stelara-cd-uc')]][.//td[contains(., '2022-07')]]
OR
//tr[contains(., 'Belgium') and contains(., 'be-stelara-cd-uc') and contains(., '2022-07')]