seleniumxpathancestor

Select child of sibling element


I have the following HTML DOM. My goal is to get the "Error" text.

<div class = "row">
   <div class ="col-sm-4">
      <input id = "firstName" type = "text"> John
      <div class = "mh-15">
         <div class = "text-danger">Error</div>
      </div>
   </div>
</div>

As class = "text-danger" is repeated numerous times, my goal is to get to the "Error" text by starting from id = "firstName" and going down the way by using something like an xpath ancestor. How should i do that? Thanks!


Solution

  • Xpath :

    //input[@id='firstName']/following-sibling::div[2]
    

    Note that <div class = "mh-15"> should be sibling of input box.