gpt4 book ai didi

python - 无法捕获异常

转载 作者:太空宇宙 更新时间:2023-11-03 13:42:38 27 4
gpt4 key购买 nike

所以我试图捕获 Webdriver 异常并且不希望它的回溯污染我的日志。这是代码的一部分

from selenium.common.exceptions import TimeoutException, WebDriverException

try:
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.loading')))
except TimeoutException:
log.msg("Seneium Timeout: {}".format(response.url))
except WebDriverException as e:
log.msg("Selenium Exception: {0} Message: {1}".format("my message", str(e)))
finally:
driver.quit()

但我仍然得到这些:

 <full traceback here>
selenium.common.exceptions.WebDriverException: Message: Can not connect to GhostDriver

我做错了什么?

最佳答案

在初始化 WebDriver 实例时,在 try/except block 之外引发异常:

driver = webdriver.PhantomJS()

仅供引用,这发生在使用 GhostDriver 启动 PhantomJS 时,引自 source code :

def start(self):
"""
Starts PhantomJS with GhostDriver.

:Exceptions:
- WebDriverException : Raised either when it can't start the service
or when it can't connect to the service
"""
try:
self.process = subprocess.Popen(self.service_args, stdin=subprocess.PIPE,
close_fds=platform.system() != 'Windows',
stdout=self._log, stderr=self._log)

except Exception as e:
raise WebDriverException("Unable to start phantomjs with ghostdriver.", e)
count = 0
while not utils.is_connectable(self.port):
count += 1
time.sleep(1)
if count == 30:
raise WebDriverException("Can not connect to GhostDriver")

而且,start()called in WebDriver's constructor (__init__() method) .

换句话说,它启动了一个服务,但无法连接到它。

关于python - 无法捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27596008/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com