https://github.com/seleniumbase/Seleniu ... _init__.py
мы можем точно увидеть, что такое driver.disconnect() и драйвер .connect() выполняет:
driver.disconnect():
Код: Выделить всё
def disconnect(self):
"""Stops the chromedriver service that runs in the background.
To use driver methods again, you MUST call driver.connect()"""
if hasattr(self, "service"):
try:
self.service.stop()
except Exception:
pass
time.sleep(0.012)
Код: Выделить всё
def connect(self):
"""Starts the chromedriver service that runs in the background
and recreates the session."""
if hasattr(self, "service"):
try:
self.service.start()
except Exception:
pass
time.sleep(0.012)
try:
self.start_session()
except Exception:
pass
time.sleep(0.012)
Мой код/попытка выглядит следующим образом:
Код: Выделить всё
with SB(uc=True, ad_block_on=True, incognito=True, do_not_track=True, maximize=True, extension_dir="ext") as sb:
url = "https://www.example.com/"
sb.driver.uc_open_with_reconnect(url, 4)
wait = WebDriverWait(sb.driver, 10)
sb.driver.disconnect()
#Connect webdriver before performing selenium action(s), example:
sb.driver.connect()
element = wait.until(EC.element_to_be_clickable((By.XPATH, my_xpath)))
#disconnect after action(s) are performed
sb.driver.disconnect()
Код: Выделить всё
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\urllib3\connection.py", line 174, in _new_conn
conn = connection.create_connection(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\urllib3\util\connection.py", line 95, in create_connection
raise err
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\urllib3\util\connection.py", line 85, 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:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\urllib3\connectionpool.py", line 715, in urlopen
httplib_response = self._make_request(
^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\urllib3\connectionpool.py", line 416, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\urllib3\connection.py", line 244, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers)
File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\http\client.py", line 1336, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\http\client.py", line 1382, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\http\client.py", line 1331, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\http\client.py", line 1091, in _send_output
self.send(msg)
File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\http\client.py", line 1035, in send
self.connect()
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\urllib3\connection.py", line 205, in connect
conn = self._new_conn()
^^^^^^^^^^^^^^^^
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\urllib3\connection.py", line 186, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: : 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 "C:\Users\User\PycharmProjects\pythonProject\sb_test.py", line 354, in
print(f'after function: {post_button.text}')
^^^^^^^^^^^^^^^^
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 90, in text
return self._execute(Command.GET_ELEMENT_TEXT)["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 395, in _execute
return self._parent.execute(command, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 352, in execute
response = self.command_executor.execute(driver_command, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 302, in execute
return self._request(command_info[0], url, body=data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 322, in _request
response = self._conn.request(method, url, body=body, headers=headers)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\urllib3\request.py", line 77, in request
return self.request_encode_url(
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\urllib3\request.py", line 99, in request_encode_url
return self.urlopen(method, url, **extra_kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\urllib3\poolmanager.py", line 376, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\urllib3\connectionpool.py", line 827, in urlopen
return self.urlopen(
^^^^^^^^^^^^^
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\urllib3\connectionpool.py", line 827, in urlopen
return self.urlopen(
^^^^^^^^^^^^^
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\urllib3\connectionpool.py", line 827, in urlopen
return self.urlopen(
^^^^^^^^^^^^^
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\urllib3\connectionpool.py", line 799, in urlopen
retries = retries.increment(
^^^^^^^^^^^^^^^^^^
File "C:\Users\User\PycharmProjects\pythonProject\.venv\Lib\site-packages\urllib3\util\retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=61788): Max retries exceeded with url: /session/b408edfdfe5527eeea10474b020410c5/element/f.82FA29D20D3690B03EAF9BB58E80412A.d.57781E4F7DCA9BCA6EA5196EDA232CF3.e.34/text (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
Process finished with exit code 1
Подробнее здесь: https://stackoverflow.com/questions/786 ... -detection