无法识别python selenium webscraping“NoSuchElementException”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19200497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:07:07  来源:igfitidea点击:

python selenium webscraping "NoSuchElementException" not recognized

pythonexceptionseleniumselenium-webdriver

提问by lollercoaster

Sometimes on a page I'll be looking for an element which may or may not be there. I wanted to try/catch this case with a NoSuchElementException, which selenium was throwing when certain HTML elements didn't exist. Original exception:

有时在页面上我会寻找可能存在或不存在的元素。我想用 a 来尝试/捕获这种情况,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/[email protected]/components/driver_component.js:8899)
    at FirefoxDriver.prototype.findChildElement (file:///var/folders/6q/7xcjtgyj32nfc2yp_y5tr9pm0000gn/T/tmp63Mz2a/extensions/[email protected]/components/driver_component.js:8911)
    at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/6q/7xcjtgyj32nfc2yp_y5tr9pm0000gn/T/tmp63Mz2a/extensions/[email protected]/components/command_processor.js:10840)
    at DelayedCommand.prototype.executeInternal_ (file:///var/folders/6q/7xcjtgyj32nfc2yp_y5tr9pm0000gn/T/tmp63Mz2a/extensions/[email protected]/components/command_processor.js:10845)
    at DelayedCommand.prototype.execute/< (file:///var/folders/6q/7xcjtgyj32nfc2yp_y5tr9pm0000gn/T/tmp63Mz2a/extensions/[email protected]/components/command_processor.js:10787) 

Ironically, it won't let me catch this exception which it was throwing before? Code here:

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

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)

Error here:

错误在这里:

NameError: name 'NoSuchElementException' is not defined

Googling/documentation came up with nothing...and it strikes me as strange that selenium is fine throwing an exception but can't catch it.

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

采纳答案by Steve Barnes

You need to import the exception first

您需要先导入异常

from selenium.common.exceptions import NoSuchElementException

and then you can reference it

然后你可以参考它

except NoSuchElementException:
    # handle the element not existing

回答by Steve Barnes

You sould use from selenium.common.exceptions import *and then write except NoSuchElementException

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