title: Selenium Continue script if Unable to locate element
body: <p>I have a problem, when I acces a website if want to check if the element is displayed and enabled. If that is the case I want to print something. If the element is not displayed and enabled I want to check for a new element, if that element is displayed if want to print something. I hope you get the point.</p>
  
  <p>When visiting a website and the first IF statement is NOT detected / displayed it gives and error instead of going to the next If statement.</p>
  
  <p>ERROR:</p>
  
  <pre><code>selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //*[@id="errorLongContent"]
  </code></pre>
  
  <p>I already tried using try, expect and changed the if statement to elif.</p>
  
  <p>I hope someone can help me.</p>
  
  <pre><code>from selenium import webdriver
  from selenium.webdriver import Firefox
  from selenium.webdriver.firefox.options import Options
  
  # Setup browser for
  options = Options()
  profile = webdriver.FirefoxProfile()
  driver = Firefox(executable_path='geckodriver', options=options, firefox_profile=profile)
  driver.get("https://agar.io")
  
  #Xpaths
  PLAY_BUTTON_XPATH = '//*[@id="play"]'
  PROXY_DENIES_CONNECTION = '//*[@id="errorLongContent"]'
  TIMEOUT_XPATH1 = '//*[@id="errorTryAgain"]'
  
  
  #checking for error
  def mainfunction():
      while True:
          print("Starting")
          if driver.find_element_by_xpath(PROXY_DENIES_CONNECTION).is_enabled() and driver.find_element_by_xpath(
                  PROXY_DENIES_CONNECTION).is_displayed():
              print("Proxy denies connection")
              driver.quit()
  
          if driver.find_element_by_xpath(TIMEOUT_XPATH1).is_enabled() and driver.find_element_by_xpath(
                  TIMEOUT_XPATH1).is_displayed():
              print("Time out detected")
              driver.quit()
  
          if driver.find_element_by_xpath(PLAY_BUTTON_XPATH).is_enabled() and driver.find_element_by_xpath(
                  PLAY_BUTTON_XPATH).is_displayed():
              print("Agar.io server is loaded")
              break
  
          else:
              continue
  
  mainfunction()
  </code></pre>
  
  <p>EDIT: Try and except</p>
  
  <pre><code>def mainfunction():
      while True:
          print("Starting")
          try:
              if driver.find_element_by_xpath(PROXY_DENIES_CONNECTION).is_enabled() and driver.find_element_by_xpath(
                      PROXY_DENIES_CONNECTION).is_displayed():
                  print("Proxy denies connection")
                  driver.quit()
          except:
              continue
          try:
              if driver.find_element_by_xpath(TIMEOUT_XPATH1).is_enabled() and driver.find_element_by_xpath(
                      TIMEOUT_XPATH1).is_displayed():
                  print("Time out detected")
                  driver.quit()
          except:
              continue
  
          try:
              if driver.find_element_by_xpath(PLAY_BUTTON_XPATH).is_enabled() and driver.find_element_by_xpath(
                      PLAY_BUTTON_XPATH).is_displayed():
                  print("Agar.io server is loaded")
                  break
          except:
              continue
  </code></pre>
  
  <p>When I run this it, runs in an infinite loop printing only Starting...</p>