I want to red text under strong tag I have written XPath like:


Here is the HTML code:

<div class="listing-info">
    <h4>Listing Information</h4>
    <strong style="">Load #:</strong> 238635<br>
    <strong>Date Listed:</strong> 5/11/17 2:12PM<br>
    <strong>Ready to Load:</strong> 5/12/17 8:00AM<br>
    <strong>Stock #:</strong> Old unit #17<br>
    <strong>Serial #:</strong> 2FZAASDC35AU00313<br>
    <strong>PO #:</strong> <br>
    <strong>Cargo Value:</strong> <br>
    <strong>Year:</strong> 2017<br>
    <strong>Shipper's Completed Jobs:</strong> (0)<br>

I want to locate the values of the strong elements:

238635, 5/11/17 2:12PM, 5/12/17 8:00AM 

I tried but was unable to read the text value.

1 Answer 1


What you expect to query is actually placed not "under" strong tag, but "next to" strong tag. There is the text nodes that you should take. Hence you should take all the text nodes having string as preceding sibling and filter out the nodes filled with white-spaces.

Use this xpath:


Tested in https://www.freeformatter.com/xpath-tester.html

Note that your xml is not well-formed so in that online tester you should make your BR tags closed.

<div class="listing-info">
    <h4>Listing Information</h4>
    <strong style="">Load #:</strong> 238635<br/>
    <strong>Date Listed:</strong> 5/11/17 2:12PM<br/>
    <strong>Ready to Load:</strong> 5/12/17 8:00AM<br/>
    <strong>Stock #:</strong> Old unit #17<br/>
    <strong>Serial #:</strong> 2FZAASDC35AU00313<br/>
    <strong>PO #:</strong> <br/>
    <strong>Cargo Value:</strong> <br/>
    <strong>Year:</strong> 2017<br/>
    <strong>Shipper's Completed Jobs:</strong> (0)<br/>

The output is:

Text=' 238635'
Text=' 5/11/17 2:12PM'
Text=' 5/12/17 8:00AM'
Text=' Old unit #17'
Text=' 2FZAASDC35AU00313'
Text=' 2017'
Text=' (0)'

UPD: According to comment provided, it seems that original html snippet didn't represent the actual state. Here is the test on original source:




enter image description here

In case you need some particular item, use approach like this:

//div[@class='equipment_item']//text()[preceding-sibling::strong[text()='Load #:']][1]

Your Answer

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

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