Search code examples
seleniumxpathsiblingsxpath-1.0ancestor

How to apply condition on child xpath from parent xpath


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>

Solution

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