Mfr Part#:

Get text content of html element using xpath

This is html content

<p class="ref   Hover">
    <b class="Hover   Hover   Hover   Hover   Hover">Mfr Part#:</b>
    MC34063ADR2G<br>
   <b>Mounting Method:</b>&nbsp;
     Surface Mount<br>
   <b>Package Style:</b>&nbsp;
     SOIC-8<br>
  <b>Packaging:</b>&nbsp;
     REEL<br>
</p> 

Using the below xpath, I can only get "Mfr Part #:".

//div[@id='product-desc']/p[2]/b[1]/text()
//div[@id='product-desc']/p[2]/b[1]

But I want "Mfr Part #: MC34063ADR2G"

+4
source share
4 answers

Your MC34063ADR2G should be

//div[@id='product-desc']/p[2]/text()[2]
+2
source

You can use the following-siblingaxis to get the text node following the element <b>that you have already selected, for example, the current context element is an element <b>:

following-sibling::text()[1]

or using full XPath:

//div[@id='product-desc']/p[2]/b[1]/following-sibling::text()[1]
+2
source

//b[contains(@class, 'Hover')]

0

, ,

String text=driver.findElement(By.xpath("//p[contains(@class, 'Hover')]")).getText();
    String[] temp=text.split("\\bMounting\\b");
    System.out.println(temp[0]);
0

All Articles