如何获取硒中特定标签的详细信息?

How to get the details of specific tag in selenium?

这是代码,我需要获取 "DDetail" 详细信息。

<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>

我尝试使用 XPath,但它无法正常工作,因为 <p> 不一致。 我正在使用 JAVA 获取此详细信息。

如果你想在Description的基础上DDetail试试下面的xPath :-

  • 使用following轴:

    (//*[contains(text(), 'Description')]/following::p)[1]
    
  • 使用preceding轴:

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

注意 :- 如果您需要其他详细信息,只需更改粗体文本,就像您想要 MDetail 只需更改上述 xpath 文本之一DescriptionMaterials

或者如果您只想基于 class 名称 style14Reg555,请尝试使用 xpath 中的索引,如下所示:-

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

如果你想获取标签 <p class="style14Reg555"> DDetail </p> 你可以使用 XPath //p[text()=' DDetail '].

您没有指定语言,下面是一些Java。

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

您可以使用 XPath 获取 "DDetail" 详细信息,java:

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