Home > front end >  How to fetch second id from <div> tag using xpath if its has multiple classes with same classn
How to fetch second id from <div> tag using xpath if its has multiple classes with same classn

Time:01-18

Observed that the only unique value is id, how can we fetch in this case.

Please Find the HTML :

<div id="0007" data-activity-type="CompatCheck" ></div>
                                
<div id="110007" data-activity-type="CompatCheck" </div>

While trying to use following code line :

findElement(By.xpath("//div[@data-activity-type='CompatCheck']")).getAttribute("id");

I'm getting only first id i.e; 0007

but I need always the second id="110007", can you please suggest to get the second id

Expected output : 110007

CodePudding user response:

In case you always need the second element id you can update your XPath accordingly.
This should work:

findElement(By.xpath("(//div[@data-activity-type='CompatCheck'])[2]")).getAttribute("id");
  • Related