简体   繁体   English

python selenium driver.quit() 里面除了块

[英]python selenium driver.quit() inside except block

My application is such that if it throws an exception I want the driver to close, I tried the following code but it is throwing exception .我的应用程序是这样的,如果它抛出异常,我希望驱动程序关闭,我尝试了以下代码,但它抛出了异常。

My code: where url is the url I want to open我的代码:其中 url 是我要打开的 url

driver=webdriver.Firefox()
try:
   driver.get(url)

except:

   driver.quit()

It is closing the driver but throwing an exception.它正在关闭驱动程序但抛出异常。 How should I fix this?我应该如何解决这个问题?

This is my stacktrace这是我的堆栈跟踪

raceback (most recent call last):
  File "/folderpath", line 47, in <module>
    driver.close()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 505, in close
    self.execute(Command.CLOSE)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 231, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 395, in execute
    return self._request(command_info[0], url, body=data)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 425, in _request
    self._conn.request(method, parsed_url.path, body, headers)
  File "/usr/lib/python2.7/httplib.py", line 973, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.7/httplib.py", line 1007, in _send_request
    self.endheaders(body)
  File "/usr/lib/python2.7/httplib.py", line 969, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 829, in _send_output
    self.send(msg)
  File "/usr/lib/python2.7/httplib.py", line 791, in send
    self.connect()
  File "/usr/lib/python2.7/httplib.py", line 772, in connect
    self.timeout, self.source_address)
  File "/usr/lib/python2.7/socket.py", line 571, in create_connection
    raise err
socket.error: [Errno 111] Connection refused

You could try using driver.close() instead.您可以尝试使用driver.close()代替。 In the example below there's no stacktrace displayed, the exception is caught and the driver/firefox window closes gracefully.在下面的示例中,没有显示堆栈跟踪,异常被捕获并且驱动程序/firefox 窗口正常关闭。

driver = webdriver.Firefox()
try:
    raise WebDriverException
except WebDriverException:
    driver.close()

or even better practice - close your driver within a finally block:甚至更好的做法 - 在 finally 块中关闭您的驱动程序:

driver = webdriver.Firefox()
try:
    raise WebDriverException
except WebDriverException:
    'Handle your exception here'
finally:
    driver.close()

After numerous requests webdriver has finally been updated to implement the context manager interface .经过无数次请求, webdriver终于被更新以实现上下文管理器接口 As of May, 2018 you can do:截至 2018 年 5 月,您可以执行以下操作:

    with webdriver.Firefox() as driver:
        driver.get("https://www.selenium.dev")
        raise WebDriverException

The context manager will take care of gracefully quitting and cleaning up.上下文管理器将负责优雅地退出和清理。 If you get an error then make sure you have the latest selenium module by running pip install -U selenium .如果出现错误,请通过运行pip install -U selenium确保您拥有最新的 selenium 模块。

The corresponding pull request: https://github.com/SeleniumHQ/selenium/pull/5919 .对应的拉取请求: https : //github.com/SeleniumHQ/selenium/pull/5919

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python selenium driver.quit() 不会中途终止程序 - python selenium driver.quit() won't terminate the program midway Selenium ChromeDriver-driver.quit()上的HTTP 407 - Selenium ChromeDriver - HTTP 407 on driver.quit() 如果设置了firefox_profile,则python selenium driver.quit()无法退出firefox浏览器 - python selenium driver.quit() can not quit firefox browser if firefox_profile setted Selenium 中的 driver.quit():ConnectionRefusedError 如果用于多个驱动程序 object - driver.quit() in Selenium : ConnectionRefusedError if used on more than one driver object 尽管 driver.close 和 driver.quit,IDLE 不会终止我的 selenium 浏览器 - IDLE doesn't terminate my selenium broswer despite driver.close and driver.quit 在 driver.quit 后调用 chromedriver 不起作用? - Invoking chromedriver after driver.quit not working? 硒3.0.2中的driver.quit firefox 50.1.0 firefox已停止工作 - driver.quit in selenium 3.0.2 firefox 50.1.0 firefox has stopped working Selenium:如何在不调用 driver.quit() 的情况下停止影响 PC 内存的 geckodriver 进程? - Selenium : How to stop geckodriver process impacting PC memory, without calling driver.quit()? driver.quit() 在未处理的异常之后 - driver.quit() after unhandled exception 在所有情况下调用 driver.quit() - invoke driver.quit() in all cases
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM