Python Selenium ConnectionRefusedError: [WinError 10061] Не удалось установить соединение, так как целевая машина активно отказалась от него

#python #selenium #selenium-webdriver #geckodriver #selenium-firefoxdriver

Вопрос:

Я использую модуль Python (3.9) Selenium (с последней версией geckodriver 0.29.1) в Windows.

Когда я запускаю этот сценарий

 from selenium import webdriver

firefox_profile = webdriver.FirefoxProfile()
driver = webdriver.Firefox(firefox_profile=firefox_profile)

print("Going into loop")

try:
    while True:
        pass
except KeyboardInterrupt:
    print("Caught exception")
finally:
    print("Finally block")
    driver.get("https://google.com")

print("Got out of loop")
 

Я получаю эту ошибку: (Я нажимаю Ctrl C в ^C)

 PS D:UsersmaartPycharmProjectstestWeb> python3.9 .test.py
Going into loop
^C
Caught exception
Finally block
Traceback (most recent call last):
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesurllib3connection.py", line 159, in _new_conn
    conn = connection.create_connection(
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesurllib3utilconnection.py", line 84, in create_connection
    raise err
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesurllib3utilconnection.py", line 74, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesurllib3connectionpool.py", line 670, in urlopen
    httplib_response = self._make_request(
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesurllib3connectionpool.py", line 392, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0libhttpclient.py", line 1253, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0libhttpclient.py", line 1299, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0libhttpclient.py", line 1248, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0libhttpclient.py", line 1008, in _send_output
    self.send(msg)
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0libhttpclient.py", line 948, in send
    self.connect()
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesurllib3connection.py", line 187, in connect
    conn = self._new_conn()
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesurllib3connection.py", line 171, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x0000020FD45F98E0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:UsersmaartPycharmProjectstestWebtest.py", line 15, in <module>
    driver.get("https://google.com")
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesseleniumwebdriverremotewebdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesseleniumwebdriverremotewebdriver.py", line 319, in execute
    response = self.command_executor.execute(driver_command, params)
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesseleniumwebdriverremoteremote_connection.py", line 374, in execute
    return self._request(command_info[0], url, body=data)
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesseleniumwebdriverremoteremote_connection.py", line 397, in _request
    resp = self._conn.request(method, url, body=body, headers=headers)
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesurllib3request.py", line 79, in request
    return self.request_encode_body(
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesurllib3request.py", line 171, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesurllib3poolmanager.py", line 336, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesurllib3connectionpool.py", line 754, in urlopen
    return self.urlopen(
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesurllib3connectionpool.py", line 754, in urlopen
    return self.urlopen(
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesurllib3connectionpool.py", line 754, in urlopen
    return self.urlopen(
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesurllib3connectionpool.py", line 726, in urlopen
    retries = retries.increment(
  File "C:UsersmaartAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesurllib3utilretry.py", line 439, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=52355): Max retries exceeded with url: /session/cc42916d-a399-45b8-90a0-a53f435e5d51/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000020FD45F98E0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
 

Почему это происходит? Я сделал этот особый случай, потому что я хочу создать программу, в которой вы можете выйти из бесконечного цикла, нажав Ctrl C, а затем я хочу вызвать driver.quit(). Это приводит к точно такой же ошибке.

Комментарии:

1. Что находится ^C в консоли ?

2. Ctrl C посылает сигнал прерывания запущенной программе, и в python этот сигнал вызывает прерывание клавиатуры

3. Зачем кому-то это делать ?

4. Ну, я хочу использовать программу selenium, которая зацикливается, пока я не захочу ее остановить. Обычно вы останавливаете программу на python,нажав ctrl c. Это единственный известный мне способ остановить программу

5. работает ли он с другим драйвером, таким как chrome?