P A N
P A N

Reputation: 5922

xPath: Select one of multiple Following elements, if it exists (OR)

Using xPath for Selenium, I want to look for an element that follows a conditional parent node. However, I want it to look for multiple possible following nodes.

Is there a way to select the element following the conditional node, as either span or p, if one of them are present?

try:
    elmnt = driver.find_element_by_xpath(
                                        ("//*[text()[contains(., 'myConditionString')]]" + 
                                        "/following::span or following::p") #← Find this 
                                        ).text
except:
    pass
else:
    print elmnt

The xPath should be looking for both:

<root>
    <b>myConditionString</b>
        <span>Find me!</span>
</root>

and this at the same time:

<root>
    <b>myConditionString</b>

    <p>Find me!</p>
</root>

Upvotes: 0

Views: 1411

Answers (1)

Andersson
Andersson

Reputation: 52685

Try following XPath and let me know if it still not correct:

driver.find_element_by_xpath('//b[text()="myConditionString"]/span[text()="Find me!"] | //b[text()="myConditionString"]/p[text()="Find me!"]')

Upvotes: 1

Related Questions