Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 504
Niranjan Dattatreya Avatar asked Oct 27 '25 14:10

Niranjan Dattatreya


2 Answers

After numerous requests webdriver has finally been updated to implement the context manager interface. As of May, 2018 you can do:

    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.

The corresponding pull request: https://github.com/SeleniumHQ/selenium/pull/5919.

like image 114
ccpizza Avatar answered Oct 29 '25 03:10

ccpizza


You could try using driver.close() instead. In the example below there's no stacktrace displayed, the exception is caught and the driver/firefox window closes gracefully.

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

or even better practice - close your driver within a finally block:

driver = webdriver.Firefox()
try:
    raise WebDriverException
except WebDriverException:
    'Handle your exception here'
finally:
    driver.close()
like image 36
AK47 Avatar answered Oct 29 '25 05:10

AK47



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!