1
votes

Xpath to find span and div elements under an ancestor element.

ancestor_element
    ---------------//span
    ---------------//div
    ---------------//input

Suppose the xpath is a_very_complex_xpath_to_find_the_ancestor_element for the ancestor_element. It is very complex.

(a_very_complex_xpath_to_find_the_ancestor_element//span) | (a_very_complex_xpath_to_find_the_ancestor_element//div)

The above union slows down the performance greatly. Is there a better Xpath for union? Tried the following:

a_very_complex_xpath_to_find_the_ancestor_element//(span | div)

It is not working.

1

1 Answers

0
votes

Need little tweak to your xpath.

a_very_complex_xpath_to_find_the_ancestor_element//*[local-name()='span' or local-name()='div']

Here we are finding the elements based on the tag name either span or div.

<html><head></head><body>
      <div id="ancestor">
			   <a>
          <label>inner</label>
         </a>
			  <span> span under ancestor </span>
			  <label> label under ancestor </label>
			  <div> div under ancestor </div>
		</div>
</body></html>

enter image description here