0

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>
jenny
  • 7
  • 7

3 Answers3

1

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

enter image description here

References:

Dmitri T
  • 119,313
  • 3
  • 56
  • 104
0

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>
  1. Find a <p> tag arbitrarily deep in the HTML document that contains the text "Pending".

  2. From the <p> that contains the text "Pending", go up three parent nodes, which takes you to the <tr> tag.

  3. From the <tr> tag, find a button that is a descendant of the <tr> tag that contains the text "wave off".

Greg Burghardt
  • 14,951
  • 7
  • 38
  • 71
0

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:

WavedOff

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

DebanjanB
  • 118,661
  • 30
  • 168
  • 217