pythonseleniumpython-3.6

Check for element without exception if element not found


I am trying to probe a page for if a particular element exists on the page as it will alter a condition in my process. I use a .find_element_by_xpath to find the element and it's works fine, but if that search is performed on a page that doesn't have the element I get an exception and process halt.

I am new to selenium and looked at the docs, but I just couldn't find what I'm looking for.

Any guidance is appreciated.

My handler:

try:
    browser.find_element_by_xpath('//*[@id="message"]')
except NoSuchElementException as e:
    print('No Such Element has occurred')
    pass

Here's the trace:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.3\helpers\pydev\pydev_run_in_console.py", line 53, in run_file
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/DWL/Desktop/comment_bot/comment_bot.py", line 187, in <module>
    advertise()
  File "C:/Users/DWL/Desktop/comment_bot/comment_bot.py", line 102, in advertise
    advert = [popper(adv_vc, add_vc, ad_comment) for x in range(len(adv_vc))]
  File "C:/Users/DWL/Desktop/comment_bot/comment_bot.py", line 102, in <listcomp>
    advert = [popper(adv_vc, add_vc, ad_comment) for x in range(len(adv_vc))]
  File "C:/Users/DWL/Desktop/comment_bot/comment_bot.py", line 120, in popper
    post_comment(a[0][0])
  File "C:/Users/DWL/Desktop/comment_bot/comment_bot.py", line 158, in post_comment
    browser.find_element_by_xpath('//*[@class="style-scope ytd-message-renderer"]')
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 385, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 955, in find_element
    'value': value})['value']
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class="style-scope ytd-message-renderer"]"}
  (Session info: chrome=64.0.3282.186)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.16299 x86_64)

Solution

  • You can use a try-except block to ignore the exception like this:

    from selenium.common.exceptions import NoSuchElementException
    
    try:
        driver.find_element_by_xpath(...)
    except NoSuchElementException:
        print("Element not found")