0

I have an HTML document with a structure like this:

<div class="parent">
  <!--more stuff here-->
    <div class="child">
    </div>
</div>

I want to select the element with the class of 'child' that sits beneath class 'parent'. Will the below get the job done in xpath, and/or are there better ways? (Note that any number of elements can sit between the <div>s above.)

//div[@class='parent']//div[@class='child']

1 Answer 1

1

If you meant to select all elements having class of child at any depth within parent, for example :

<div class="parent">
  <!--more stuff here-->
    <div class="child">
    </div>
    <div class="other">
        <div class="child">
        </div>
    </div>
</div>

..then yes, your current XPath will do, both child in above sample will be selected. Or in case you only want direct childs of parent you can use single slash :

//div[@class='parent']/div[@class='child']
Sign up to request clarification or add additional context in comments.

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.