Home > database >  Selenium Python - Using XPATH with Or operator
Selenium Python - Using XPATH with Or operator

Time:08-21

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.

CodePudding user response:

A much simpler expression would be:

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

CodePudding user response:

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

CodePudding user response:

You may use |, the pipe symbol, to denote or.

This should work:

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