larryq
larryq

Reputation: 16309

selector for xpath / css subclass

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']

Upvotes: 0

Views: 1269

Answers (1)

har07
har07

Reputation: 89285

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']

Upvotes: 1

Related Questions