-1

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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
bbfl
  • 277
  • 1
  • 2
  • 16
  • The question itself is incorrect as in first case `find_logo` returns `list` and in second - single WebElement. So its not about *what approach is correct* but *what output user wants to get* – DonnyFlaw Nov 25 '20 at 09:30
  • Oh yes, the problem is that if it is ```find_element``` and logo image is not there then it will always return error unable to find such element and will not get to the else statement. – bbfl Nov 25 '20 at 09:54
  • Sooo... Obviously, you need to use `try`/`except` in this case – DonnyFlaw Nov 25 '20 at 09:57

2 Answers2

1

Canonically both the if-else and try-except logic needs to be revamped however they needs to be implemented as per your usecase.


if find_logo(): The if find_logo() will be always successful as in def find_logo() you have used find_elements() which may not return any elements at all. In such case you may require to check if the list size is not 0.


try: find_logo(): try: find_logo() seems to be better organized in comarision to the other with NoSuchElementException check in place when the specific element isn't found.


Code optimization

However as per best practices, when looking for an element, you need to induce WebDriverWait for the visibility_of_element_located() and you can use the following logic:

try:
    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".navbar-brand [src='/logo/logo.svg']")))
    print("Found the logo")
except TimeoutException:
    print("Cannot find the logo")
    

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for the good explanation however I have one more question why WebDriverWait for visibility of element better than find_element? Isn't it the same or...? – bbfl Dec 02 '20 at 15:34
  • 1
    @bbfl [Checkout](https://stackoverflow.com/questions/59130200/selenium-wait-until-element-is-present-visible-and-interactable/59130336#59130336) these [discussions](https://stackoverflow.com/questions/52603847/how-to-sleep-webdriver-in-python-for-milliseconds/52607451#52607451) on [WebDriverWait](https://stackoverflow.com/questions/49775502/webdriverwait-not-working-as-expected/49775808#49775808) – undetected Selenium Dec 02 '20 at 15:38
-1

In first case function return a list and in the second case, function return that web element that you are looking for.

  • Oh yes, the problem is that if it is ```find_element``` and logo image is not there then it will always return error unable to find such element and will not get to the else statement. – bbfl Nov 25 '20 at 09:54