I am trying to find xpath of text 262567 of given below HTML code :

<div class="lessthanhalf" >
  <h4> Listing</h4>
  <strong style="background: rgb(204, 136, 136); border: 2px solid red;">Load #:</strong>
<strong style="background: rgb(204, 136, 136); border: 2px solid red;">Date Listed:</strong>

Tried with //div[@class='lessthanhalf']/h4/following-sibling::strong/text()[3] but not getting success.

Can someone please help me on that.


You can use below xpath to get the text what you want to get


But selenium doesn't allow you to locate an element using text node. You can use JavascriptExecutor to evaluate your xpath to get your desired text

JavascriptExecutor js = (JavascriptExecutor)driver;  
Object load= js.executeScript("var value = document.evaluate(\"//div[@class='lessthanhalf']/strong[cont‌​ains(text(),'Load')]‌​/following::text()[1‌​]\",document, null, XPathResult.STRING_TYPE, null ); return value.stringValue;"); 
System.out.println("Load Number : "+ load.toString());
  • I have tried with java script executor but nothing is printing for text value.. – manish kumar Dec 19 '17 at 7:21
  • Can you replace //div[@class='lessthanhalf']/text()[3] in that and let me know – NarendraR Dec 19 '17 at 8:42
  • Hey @NarendraR can i get element into List by using javaScriptexecutor if multiple similar kind of elements using text node on the page. – manish kumar Dec 24 '17 at 1:57


"This worked for me"

  • 2
    Your answer would be much better if you could edit it to explain how the XPath you are using works. That way future users would be able to learn more about Xpaths from your answer instead of just taking a specific answer to a problem. – Kate Paulk Aug 22 '18 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.