Check for element without exception if element not found

女生的网名这么多〃 提交于 2020-03-25 19:25:23

问题


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)

回答1:


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")



回答2:


According to the stack trace the line throwing the error is where you are looking for xpath //*[@class="style-scope ytd-message-renderer"], not the code inside the try except block.

Another alternative is to use find_elements and check if the size is not 0

elements = browser.find_elements_by_xpath('//*[@class="style-scope ytd-message-renderer"]')
if (len(elements) > 0):
    # do something


来源:https://stackoverflow.com/questions/49216866/check-for-element-without-exception-if-element-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!