So I am using find_element to define a variable like so

 WinCondition = driver.find_element(By.XPATH, "//div[@class='bonus-game-state back bonus-game-end red']")

However the element is dynamic, this is intentional, but when the code does not find the element, it throws an exception and ends the whole process, I simply want it to do nothing if it does not find the element, because sometimes the element is on the page, sometimes it isn't

I only want it to do something if it finds the element, and nothing if it doesn't. Makes sense?

How can I prevent it from throwing the exception?

1

There are 1 best solutions below

0
On

check if an element exists then do something otherwise skip it.

try:    
    if winCondition.exists():
      do somthing
    else:
       skip it.
except:
    print("element not exists")