1

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.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Your "or" operator as written is part of your Python code, not part of the XPath expression, because it's outside the double-quotes. – Michael Kay Aug 21 '22 at 10:43

2 Answers2

2

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'.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
1

A much simpler expression would be:

R = driver.find_element(By.XPATH,"//*[contains(., 'Word1') or contains(., 'Word2')]")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352