Hiten
Hiten

Reputation: 877

How to get the details of specific tag in selenium?

This is the code and I need to get the "DDetail" details.

<div style="float:left;padding-left: 7%">
   <h2 class="lotnumber style19Bold222" style="float:left"> LOT DETAILS </h2>
   <br>
   <p class="details style14SemiBold222 styleCapitalize" style="line-height:22px;">
      <b class="boldDetails">Materials:</b>
   </p>
   <p class="style14Reg555"> MDetail </p>
   <p class="details style14SemiBold222 styleCapitalize" style="line-height:22px;">
      <b class="boldDetails">Measurements:</b>
   </p>
   <p class="style14Reg555"> MeDetail </p>
   <p class="details style14SemiBold222 styleCapitalize" style="line-height:22px;">
      <b class="boldDetails">Size Notes:</b>
   </p>
   <p class="style14Reg555"> total height </p>
   <p class="details style14SemiBold222 styleCapitalize" style="line-height:22px;">
      <b class="boldDetails">Description:</b>
   </p>
   <p class="style14Reg555"> stepped square reverse </p>
   <p class="details style14SemiBold222 styleCapitalize" style="line-height:22px;">
      <b class="boldDetails">Markings:</b>
   </p>
   <p class="style14Reg555"> MarDetail </p>
   <p class="details style14SemiBold222 styleCapitalize" style="line-height:22px;">
      <b class="boldDetails">Condition:</b>
   </p>
   <p class="style14Reg555"> CDetail </p>
</div>

I tried using XPath but it's not working properly because <p> is not consistent. I am using JAVA to get this details.

Upvotes: 0

Views: 1044

Answers (3)

Shi Linjie
Shi Linjie

Reputation: 1

You can use XPath to get the "DDetail" details , java:

  driver.findElement(By.xpath("html/body/div[1]/p[8]"))

Upvotes: 0

JeffC
JeffC

Reputation: 25596

If you want to get the tag <p class="style14Reg555"> DDetail </p> you can use the XPath //p[text()=' DDetail '].

You didn't specify a language, so here's some Java below.

driver.findElement(By.xpath("//p[text()=' DDetail ']"));

Upvotes: 1

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

If you want DDetail on the the basis of Description try below xPath :-

  • Using following axis :

    (//*[contains(text(), 'Description')]/following::p)[1]
    
  • Using preceding axis :

    (//p[preceding::b[contains(text(), 'Description')]])[1]
    

Note :- If you want other details Just change the bold text like if you want MDetail just change on one of the aboive xpath text Description to Materials

Or If you want only on the basis of class name style14Reg555, try using index in xpath as below :-

(//p[@class = 'style14Reg555'])[4]

Upvotes: 1

Related Questions