3

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.

1 Answer 1

6

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
    
1
  • Thanks! works like a charm! Commented Jan 14, 2020 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.