I am confused which is the best and correct way to check if element exists or not? Using try-except
or if-else
? What would be the difference and what are advantages/disadvantages of both when trying to find element?
def find_logo():
return driver.find_elements(By.CSS_SELECTOR, ".navbar-brand [src='/logo/logo.svg']")
if find_logo():
print("Found the logo")
else:
print("Cannot find the logo")
So will get the same result with try except:
def find_logo():
return driver.find_element(By.CSS_SELECTOR, ".navbar-brand [src='/logo/logo.svg']")
try:
find_logo()
print("Found the logo")
except NoSuchElementException:
print("Cannot find the logo")
Both seem to work the same way but which is the correct way to do it?