I am doing some screen scraping with a library that takes XPath expressions and noticed that several pages are similar, but different.
Is there a way to loosely say “get me divs that have class=’mytarget’ but exist as a child of a div with class = ‘nav’ and the exact path is unknown between nav and mytarget.”
<div class="nav">
<div>
??????
<div class="mytarget"></div>
??????
</div>
</div>
Yes, using the
descendant-or-selfaxis (//):Or, if there can be more than one class name on those elements, then this is even better:
Warning: this can be very inefficient on large documents. You should use absolute paths wherever the structure is known. Only resort to
//when the structure is unknown.