0

Please find the HTML :

<div id="086" data-activity-type="CompatCheck" class="Activity"></div>

Here only constant value is data-activity-type="CompatCheck" and classname is same for all the other elements.

while trying to print id using data-activity-type i.e. CompatCheck using xpath

Expected output:

086

Problem 2 : 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" class="Activity"></div>
                                
<div id="110007" data-activity-type="CompatCheck" class="Activity"</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

nayansFosgit
  • 69
  • 1
  • 7

2 Answers2

1

With XPath or CSS Selectors you can locate web element based on any constant attribute value. I.e. it can be based on constant class name value or id or any other attribute. In your case the constant attribute value is data-activity-type="CompatCheck". So, you can locate this element by XPath or by CSS Selector and then extract the id attribute as following:
XPath

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

CSS Selector

driver.findElement(By.cssSelector("div[data-activity-type='CompatCheck']")).getAttribute("id");
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • @undetected Selenium & Prophet the code mentioned from your side it worked, but i'm facing another issue on the same i'm getting always the first id but i needed only the second id , I've edited and mentioned in the problem statement 2 could you please look into it,thanks – nayansFosgit Jan 17 '23 at 09:31
  • You need to ask a new, separate question for that. initially asked question is already answered and resolved here – Prophet Jan 17 '23 at 10:11
  • Hi @Prophet i have raised a new question kindly please look into it https://stackoverflow.com/questions/75148673/how-to-fetch-second-id-from-div-tag-using-xpath-if-its-has-multiple-classes-wi – nayansFosgit Jan 17 '23 at 15:28
  • I see. Already answered there. Let me know if that worked or you still need some kind of fixes / clarifications there – Prophet Jan 17 '23 at 15:30
  • yeah that worked but I'm still not getting relevant output , have raised a new case could you please look into it https://stackoverflow.com/questions/75152993/how-to-fetch-id-from-div-tag-using-xpath-in-efficient-way – nayansFosgit Jan 17 '23 at 22:52
1

As the attribute data-activity-type="CompatCheck" is unique you can use it and you can use either of the following the following locator strategies::

  • Using cssSelector:

    System.out.println(driver.findElement(By.cssSelector("div[data-activity-type='CompatCheck']")).getAttribute("id"));
    
  • Using xpath:

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

Console output:

086
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352