【问题标题】:python selenium webscraping "NoSuchElementException" not recognizedpython selenium webscraping“NoSuchElementException”无法识别
【发布时间】:2013-10-12 14:09:03
【问题描述】:

有时我会在页面上寻找可能存在或不存在的元素。我想用NoSuchElementException 尝试/捕捉这种情况,当某些 HTML 元素不存在时,硒会抛出这种情况。原始异常:

NoSuchElementException: Message: u'Unable to locate element: {"method":"css selector","selector":"#one"}' ; Stacktrace: 
    at FirefoxDriver.prototype.findElementInternal_ (file:///var/folders/6q/7xcjtgyj32nfc2yp_y5tr9pm0000gn/T/tmp63Mz2a/extensions/fxdriver@googlecode.com/components/driver_component.js:8899)
    at FirefoxDriver.prototype.findChildElement (file:///var/folders/6q/7xcjtgyj32nfc2yp_y5tr9pm0000gn/T/tmp63Mz2a/extensions/fxdriver@googlecode.com/components/driver_component.js:8911)
    at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/6q/7xcjtgyj32nfc2yp_y5tr9pm0000gn/T/tmp63Mz2a/extensions/fxdriver@googlecode.com/components/command_processor.js:10840)
    at DelayedCommand.prototype.executeInternal_ (file:///var/folders/6q/7xcjtgyj32nfc2yp_y5tr9pm0000gn/T/tmp63Mz2a/extensions/fxdriver@googlecode.com/components/command_processor.js:10845)
    at DelayedCommand.prototype.execute/< (file:///var/folders/6q/7xcjtgyj32nfc2yp_y5tr9pm0000gn/T/tmp63Mz2a/extensions/fxdriver@googlecode.com/components/command_processor.js:10787) 

具有讽刺意味的是,它不会让我捕捉到它之前抛出的这个异常?代码在这里:

elt = driver.find_element_by_css_selector('.information')
try:
    dat1 = elt.find_element_by_css_selector('#one').text
    dat2 = elt.find_elements_by_css_selector('#two')[1].text
    text = dat1 + dat2
except NoSuchElementException:
    text = elt.find_element_by_css_selector('#all').text
    item.set_description(text)

此处出错:

NameError: name 'NoSuchElementException' is not defined

谷歌搜索/文档一无所获……让我感到奇怪的是,selenium 可以抛出异常但无法捕获它。

【问题讨论】:

    标签: python exception selenium selenium-webdriver


    【解决方案1】:

    需要先导入异常

    from selenium.common.exceptions import NoSuchElementException
    

    然后你就可以参考了

    except NoSuchElementException:
        # handle the element not existing
    

    如果您想在输出中提供异常的详细信息,则可以使用:

    except NoSuchElementException as exc:
        print(exc) # and/or other actions to recover 
    

    【讨论】:

    • 最好只导入您可能使用的异常还是导入整个异常类?
    • 我个人觉得保留一个命名空间通常是一个好主意,所以import selenium.common.exceptions as selexcept 然后使用selexcept.NoSuchElementException 标志给读者,以后可能是你自己包特定的异常。
    • 哇有点感觉我必须这样做,但认为它们必须默认包含异常 xD。 tytyty
    • 我导入了异常,但它仍然只记录Message: Unable to locate element: ...,如果它没有明确提及NoSuchElementException
    • @oldboy 我已经更新了答案以显示如何获取特定异常的详细信息。
    【解决方案2】:

    你应该使用from selenium.common.exceptions import *,然后写except NoSuchElementException

    【讨论】:

    • 请不要。只需导入您需要的。
    • 导入所有 (*) 不是一个好习惯。只需选择您实际需要的那些
    • 为什么只是“import selenium.common.exceptions”不能工作?有趣..
    • @FuadAk 如果您使用import selenium.common.exceptions,那么您需要提供完全限定名称selenium.common.exceptions.NoSuchElementException,当然您可以使用import selenium.common.exceptions as sex,然后您可以使用sex.NoSuchElementException 等:-)
    猜你喜欢
    • 2021-12-08
    • 2019-01-18
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    相关资源
    最近更新 更多