1

I want to read the Tax Price using the sibling concept, so I have I've written below XPath, but it's not working

My code:

//div[@class='grid_3 d-grid_10']//label[contains(text(), 'Tax')]/following-sibling::div

HTML:

<div class="grid_3 d-grid_10"> 
    <label class="m-confirmation-modal-print-detail-capgrey"> Tax:</label>
</div> 
<div class="grid_1 d-grid_2"> 
    <label class="m-confirmation-modal-print-price text-align-right"> $10.50</label> 
</div>
1
  • What is the element you are trying to locate? post the html Commented Nov 20, 2018 at 9:43

3 Answers 3

2

To read the Tax Price i.e. $10.50 using text Tax within the ancestor node, you need to locate the <label> node with text as Tax: first. Then with respect to this node you need to locate the following <div> node which have a decedent node containing the required text i.e. $10.50 and to achieve that you can use the following solution:

  • XPath:

    //label[@class='m-confirmation-modal-print-detail-capgrey' and contains(.,'Tax')]//following::div[1]/label
    
Sign up to request clarification or add additional context in comments.

Comments

0

The second <div> is a sibling of the first one, not of the child <label>. You need to go back to the parent <div> first using .. or parent::div

//div[@class='grid_3 d-grid_10']//label[contains(text(), 'Tax')]/parent::div/following-sibling::div

As suggested in the comments you can simplify it by starting the xpath with the "Tax" <label>

//label[contains(text(), 'Tax')]/parent::div[@class='grid_3 d-grid_10']/following-sibling::div

1 Comment

This kind of "recursion" : parent->child->parent can be simplified: //label[contains(text(), 'Tax')]/parent::div[@class='grid_3 d-grid_10']/following-sibling::div - no need to specify the same parent div twice
0

You can use this :

//label[contains(text(), 'Tax')]/../following-sibling::div

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.