最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Selenium Python: Driver isn't being initialized properly when used with a Chrome Profile - Stack Overflow

programmeradmin5浏览0评论

I had a version of the following code that had to be run on multiple users' Windows computers and be logged in with their Google accounts while performing the actions. Therefore, I chose to directly use the default Chrome profile and this had worked for a while to address that step of the process.

chrome_options = Options()
user_data_dir = os.path.expanduser("~\\AppData\\Local\\Google\\Chrome\\User Data").replace("\\","/")  

chrome_options.add_argument(f"--user-data-dir={user_data_dir}")
chrome_options.add_argument("--profile-directory=Default")
chrome_options.add_argument("--remote-debugging-pipe")
chrome_options.add_argument("--enable-logging")
chrome_options.add_argument("--log-level=0")

service = Service(executable_path="chromedriver.exe")

driver = webdriver.Chrome(service=service, options=chrome_options)

driver.get('')

I have recently updated my ChromeDriver from 131 to 133 as my Chrome was running on 133. I was already getting the first 4-5 of the following error messages from ChromeDriver before the update but it was able to initialize the driver and perform my inputs even though my ChromeDriver and Chrome versions weren't matching. Now, when running the same code I get the Chrome window for a while with the profile but any actions afterwards are not being performed.

[15784:6412:0219/110942.360:ERROR:usb_descriptors(146)] Failed to read length for configuration 1.
[15784:6412:0219/110942.360:ERROR:usb_descriptors(105)] Failed to read all configuration descriptors. Expected 2, got 1.
[15784:6412:0219/110942.361:ERROR:device_event_log_impl(199)] [11:09:42.362] USB: usb_device_win:95 Failed to read descriptors from \\?\usb#vid_0bda&pid_8153#000001#{a5dcbf10-6530-11d2-901f-00c04fb951ed}.
[15784:9928:0219/110942.399:ERROR:connection_factory_impl(434)] Failed to connect to MCS endpoint with error -111
Created TensorFlow Lite XNNPACK delegate for CPU.
[15784:9928:0219/110943.601:ERROR:registration_request(291)] Registration response error message: DEPRECATED_ENDPOINT
[15784:9928:0219/111004.433:ERROR:registration_request(291)] Registration response error message: DEPRECATED_ENDPOINT
[2264:7264:0219/111012.233:ERROR:ssl_client_socket_impl(877)] handshake failed; returned -1, SSL error code 1, net_error -100
[2264:7264:0219/111012.400:ERROR:ssl_client_socket_impl(877)] handshake failed; returned -1, SSL error code 1, net_error -100
[15784:9928:0219/111012.400:ERROR:connection_factory_impl(434)] Failed to connect to MCS endpoint with error -100
[15784:9928:0219/111052.482:ERROR:registration_request(291)] Registration response error message: DEPRECATED_ENDPOINT
[15784:9928:0219/111053.654:ERROR:connection_factory_impl(434)] Failed to connect to MCS endpoint with error -111
[15784:9852:0219/111151.138:ERROR:devtools_pipe_handler(188)] Connection terminated while reading from pipe
Traceback (most recent call last):
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\connectionpool.py", line 534, in _make_request
    response = conn.getresponse()
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\connection.py", line 516, in getresponse
    httplib_response = super().getresponse()
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\http\client.py", line 1428, in getresponse
    response.begin()
    ~~~~~~~~~~~~~~^^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\http\client.py", line 331, in begin
    version, status, reason = self._read_status()
                              ~~~~~~~~~~~~~~~~~^^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\http\client.py", line 292, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
               ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\socket.py", line 719, in readinto
    return self._sock.recv_into(b)
           ~~~~~~~~~~~~~~~~~~~~^^^
TimeoutError: timed out

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\MyUser\Documents\svg\test.py", line 24, in <module>
    driver = webdriver.Chrome(service=service, options=chrome_options)
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__
    super().__init__(
    ~~~~~~~~~~~~~~~~^
        browser_name=DesiredCapabilities.CHROME["browserName"],
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<3 lines>...
        keep_alive=keep_alive,
        ^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 66, in __init__
    super().__init__(command_executor=executor, options=options)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 250, in __init__
    self.start_session(capabilities)
    ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 342, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
               ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 427, in execute
    response = selfmand_executor.execute(driver_command, params)
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 404, in execute
    return self._request(command_info[0], url, body=data)
           ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 428, in _request
    response = self._conn.request(method, url, body=body, headers=headers, timeout=self._client_config.timeout)
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\_request_methods.py", line 143, in request
    return self.request_encode_body(
           ~~~~~~~~~~~~~~~~~~~~~~~~^
        method, url, fields=fields, headers=headers, **urlopen_kw
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\_request_methods.py", line 278, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
           ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\poolmanager.py", line 443, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\connectionpool.py", line 841, in urlopen
    retries = retries.increment(
        method, url, error=new_e, _pool=self, _stacktrace=sys.exc_info()[2]
    )
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\util\retry.py", line 474, in increment
    raise reraise(type(error), error, _stacktrace)
          ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\util\util.py", line 39, in reraise
    raise value
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen
          ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\util\util.py", line 39, in reraise
    raise value
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen
    response = self._make_request(
          ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\util\util.py", line 39, in reraise
    raise value
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen
          ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\util\util.py", line 39, in reraise
    raise value
          ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\util\util.py", line 39, in reraise
          ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\util\util.py", line 39, in reraise
          ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\util\util.py", line 39, in reraise
    raise value
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen
    response = self._make_request(
        conn,
    ...<10 lines>...
        **response_kw,
    )
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\connectionpool.py", line 536, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\connectionpool.py", line 367, in _raise_timeout
    raise ReadTimeoutError(
        self, url, f"Read timed out. (read timeout={timeout_value})"
    ) from err
urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='localhost', port=57644): Read timed out. (read timeout=120)

When I run it without the profile I get the following and it successfully performs the actions:

[14896:8384:0219/113521.167:ERROR:usb_descriptors(146)] Failed to read length for configuration 1.
[14896:8384:0219/113521.167:ERROR:usb_descriptors(105)] Failed to read all configuration descriptors. Expected 2, got 1.
[14896:8384:0219/113521.167:ERROR:device_event_log_impl(199)] [11:35:21.180] USB: usb_device_win:95 Failed to read descriptors from \\?\usb#vid_0bda&pid_8153#000001#{a5dcbf10-6530-11d2-901f-00c04fb951ed}.
Created TensorFlow Lite XNNPACK delegate for CPU.

I have tried going back to the older ChromeDriver version again to see if it would work again but it tells me now that it only works with Chrome version 131. I have also tried to update to a marginally newer version of ChromeDriver but that also didn't work.

I also tried to copy the user data folder to a new location and rename the default folder to Profile 1 and use it that way but that also gave the same end result.

发布评论

评论列表(0)

  1. 暂无评论