Home > database >  Handle python selenium webdriver Connection Refused error. (Chrome)
Handle python selenium webdriver Connection Refused error. (Chrome)

Time:11-05

I can't understand how to catch exception of webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(
    "--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--lang=en-GB")
chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--proxy-server={}'.format(proxy))
driver = webdriver.Chrome(service=CHROMEDRIVER_PATH, options=chrome_options)

try:
     #here some things with webdriver
except Exception as e:
     #here I want to quit webriver if some exception will come
     driver.quit()

As I understand try-except block must handle all exceptions, but errors like

HTTPConnectionPool(host='localhost', port=56963): Max retries exceeded with url: /session/61d7c70479adfb73b0c8d6aecf0cdb2e/element (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7ff90feeafd0>: Failed to establish a new connection: [Errno 111] Connection refused'))
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 175, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 95, in create_connection
    raise err
  File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 85, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 710, in urlopen
    chunked=chunked,
  File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib64/python3.7/http/client.py", line 1369, in getresponse
    response.begin()
  File "/usr/lib64/python3.7/http/client.py", line 310, in begin
    version, status, reason = self._read_status()
  File "/usr/lib64/python3.7/http/client.py", line 279, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

killing script and chromium process still live in background. cmd after sript killed Can you help me please how to catch this errors and continue script? Or how to quit webdriver if this errors come?

Thanks for answers!

In google I found much answers about user agent and work with webdriver after using quit(), but I don't do this and use user agent for it.

CodePudding user response:

Your problem is that the kind of exception you are getting is not under Exception type of exceptions. You can do this in order to get what you want.

You can try this:

# Needed libs
import sys

try:
    #here some things with webdriver, in my example is divide by 0
    1 / 0
except:
    e = sys.exc_info()
    print(f"Error: {e[0]}")
    print(f"Error: {e[1]}")
    print(f"Error: {e[2]}")
  • Related