Make an if/then statement utilizing a non present element



  • I'm using Python and I came across one problem. I'm trying to make a bot using selenium, and it's coming along nicely but I'm trying to use a non-present element to activate a function and I just can't figure it out

    if (element) not present:
       (action)
    

    I can't find anything on the internet.



  • You can either:

    • Look for all element that match certain search criteria:

      if driver.find_elements_by_css_selector(my_locator):
          # No element found here
      
    • Catch an exception when searching for a single instance:

      try:
         webdriver.find_element_by_css_selector(my_locator)
         except NoSuchElementException:
             # No element found here
      


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2