if element or another_element exists in Python Selenium

  element, find, if-statement, python, selenium

I have a page where both of the following elements or only the one will be visible. How can I check if any of these two elements exist? My code is for example:

def element_one():
   driver.find_element(By.CSS_SELECTOR, ".selector_one").is_displayed()

def element_two():
   driver.find_element(By.CSS_SELECTOR, ".selector_two").is_displayed()

if element_one() or element_two():
   print("OK")
else:
   print("ERROR")

Doesn’t work for me if also tried .is_displayed() but still not working if the element_one is not visible but element_two is visible. The if or statement doesn’t reach to check if element_two is displayed. Basically only checks for element_one and if it is not there then prints ERROR without checking for element_two.

Source: Python Questions

LEAVE A COMMENT