-1

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>
    262567
  <br>
<strong style="background: rgb(204, 136, 136); border: 2px solid red;">Date Listed:</strong>
 12/14/2017
<br>

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

Can someone please help me on that.

0

2 Answers 2

1

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

//div[@class='lessthanhalf']/strong[contains(text(),'Load')]‌​/following::text()[1‌​] 

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());
3
  • I have tried with java script executor but nothing is printing for text value.. Dec 19, 2017 at 7:21
  • Can you replace //div[@class='lessthanhalf']/text()[3] in that and let me know
    – NarendraR
    Dec 19, 2017 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. Dec 24, 2017 at 1:57
-1

//div[@class='lessthanhalf']/strong[1]/following::text()[1]

"This worked for me"

1
  • 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, 2018 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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