Selenium Python - Using XPATH with Or operator

Viewed 53

I'm having an issue with using XPATH with the Or operator. (Using Selenium in Python)

R = driver.find_element(By.XPATH,"//* [contains(text(),'Word1')]" or "//* [contains(text(),'Word2')]")

Currently the code is only looking for Word1 and not Word2. Would like for it search for Word1 and if Word1 doesn't exist, look for Word 2.

I would appreciate any feedback.

2 Answers

This XPath,

//*[text()[contains(.,'Word1') or contains(.,'Word2')]

will select all elements that contain an immediate text node that contains the substring 'Word1' or the substring 'Word2'.

A much simpler expression would be:

R = driver.find_element(By.XPATH,"//*[contains(., 'Word1') or contains(., 'Word2')]")
Related