Automation Selenium - Dynamic XPath



  • In the below XPath, the index [1] is changing every time. Can we handle this?

    //button[contains(text(),'Action')][1]
    

    HTML:

    <button aria-expanded="false" aria-haspopup="true" class="dropdown-toggle component--button btn--reset" data-toggle="dropdown" type="button">
    <span aria-hidden="true" class="icon component--icon icon-angle-down">
    </span> Action</button>
    


  • If there is only one element of tag "button" and text "action" then use:

    //button[contains(text(),'Action')]
    

    If, on the other hand, the above is true AND there are multiple instances of that button, AND you want the first one, surround it first with parenthesis, then the subscript:

    (//button[contains(text(),'Action')])[1]
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2