1

Sample html:

<div>
  <div class="foo bar baz">                          <-- target 1 -->
    <div>
      <span>
        <a href="helloworld.com">hello world</a>
      </span>
    </div>
  </div>
  <div class="foo bar">foo</div>
  <div class="bar">                                  <-- target 2 -->
    <div>
      <div>
        <span>
          <a href="helloworld2.com">hello world</a>
        </span>
      </div>
    </div>
  </div>
</div>

I want to select: divs that: 1)has class name bar 2) has an <a> descendant whose href contains hello.

My problem is that the <a> tag could be nested in different levels. How to handle this correctly?

wlnirvana
  • 1,811
  • 20
  • 36

1 Answers1

1

You can use relative descendant-or-self axis (.//) to check <a> element in various possible level depth :

//div[contains(concat(' ', @class, ' '), ' bar ')][.//a[contains(@href,'hello')]]

related discussion : How can I find an element by CSS class with XPath?

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137