Reputation: 7
I wanted to click on the "waved off" button (child node) if status is "Pending" (parent node).
Then what will be the xpath in protractor ?
I've founded root xpath of waved off button but I'm not getting how to use that in coding.
if I approve first tr tag then it goes down and 2nd tr tag comes on top if it's pending. There are total 5 tr tags in my code. and all tr tags are not fixed on their position. whenever I run my script I get pending tr tags on top .
<div>
<table>
<tbody>
<tr>
<td>
<p> required </p>
</td>
<td>
<p> Pending </p>
</td>
<td>
<div>
<img> </img>
<div>
<div> </div>
<div> </div>
<button> View Remark </button>
<button> Waved off </button>
</div>
</div>
</td>
</tr>
<tr>
<td>
<p> required </p>
</td>
<td>
<p> Pending </p>
</td>
<td>
<div>
<img> </img>
<div>
<div> </div>
<div> </div>
<button> View Remark </button>
<button> Waved off </button>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
Upvotes: 0
Views: 810
Reputation: 193058
Factually, there are 2 <button>
with text as Waved off with respect to the <p>
nodes with text as Pending. So locate the desired elements you can use the following Locator Strategy:
Xpath:
//p[contains(., 'Pending')]//following::td[1]//button[contains(., 'Waved off')]
Snapshot:
Note: As mentioned above this Locator Strategy will identify 2 elements and as your usecase is to click on the desired element you have to induce a waiter
for the element to be clickable
Upvotes: 0
Reputation: 168002
It would be something like:
//p[contains(text(), 'Pending')]/parent::td/following-sibling::*/descendant::*/button[contains(text(),'Waved off')]
Going forward please include the whole <table>
tag contents into your question, the chance of getting more accurate advice will be much higher
References:
Upvotes: 1
Reputation: 18783
You can use the ..
expression to choose a parent node relative to a currently selected node in XPath. If you can get the "Pending" <p>
tag, then you just need to go up 3 tags, and then find a button that contains "waved off":
//p[contains(., 'Pending')]/../../..//button[contains(., 'waved off')]
\________________________/ \_____/ \________________________________/
| | |
(1) (2) (3)
<p> +--> <tr> <button>
Pending | <td> waved off
</p> | <p> </button>
| required
| </p>
| </td>
| <td>
+----<-- <p>
Pending
</p>
</td>
<td>
<div>
<img>
<div>
<button>
waved off
</button>
</div>
</div>
</td>
Find a <p>
tag arbitrarily deep in the HTML document that contains the text "Pending".
From the <p>
that contains the text "Pending", go up three parent nodes, which takes you to the <tr>
tag.
From the <tr>
tag, find a button that is a descendant of the <tr>
tag that contains the text "wave off".
Upvotes: 0