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


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 Answers

supputuri On Best Solutions

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.

      <div id="ancestor">
     <span> span under ancestor </span>
     <label> label under ancestor </label>
     <div> div under ancestor </div>

enter image description here