python-3.x 添加User-data-Dir后,什么可能导致Selenium中出现错误?

kb5ga3dv  于 2023-03-13  发布在  Python
关注(0)|答案(1)|浏览(240)

我基本上想在Selenium WebDriver中设置我的默认配置文件。在我重置窗口之前,相同的代码也在工作,但在重置之后,它给出了一个错误。以下是代码。

from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service

chrome_userdata_dir = 'C:\\Users\\abdul\\AppData\\Local\\Google\\Chrome\\User Data\\Default'
option = Options()
option.add_argument(f"--user-data-dir={chrome_userdata_dir}")
option.add_argument('--profile-directory=Default')
driver = Chrome(service=Service(ChromeDriverManager().install()), options=option)

如果我现在运行它,它不会打开Chrome浏览器的默认配置文件。它随意打开它,就像没有默认配置文件。如果我改变chrome_user_dir的路径如下,它给出一个错误,但打开chrome浏览器的默认配置文件。

chrome_userdata_dir = 'C:\\Users\\abdul\\AppData\\Local\\Google\\Chrome\\User Data'

错误日志如下

Opening in existing browser session.
Traceback (most recent call last):
  File "E:\LinuxData\Documents\TwitchVideoAutomator\main.py", line 135, in <module>
    automate_yt_pals(_headless)
  File "E:\LinuxData\Documents\TwitchVideoAutomator\subpal_subscribers\automate_yt_pals.py", line 56, in automate_yt_pals
    driver = Chrome(service=Service(path), options=option)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\LinuxData\Documents\TwitchVideoAutomator\venv\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 80, in __init__
    super().__init__(
  File "E:\LinuxData\Documents\TwitchVideoAutomator\venv\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 104, in __init__
    super().__init__(
  File "E:\LinuxData\Documents\TwitchVideoAutomator\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 286, in __init__
    self.start_session(capabilities, browser_profile)
  File "E:\LinuxData\Documents\TwitchVideoAutomator\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 378, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\LinuxData\Documents\TwitchVideoAutomator\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
    self.error_handler.check_response(response)
  File "E:\LinuxData\Documents\TwitchVideoAutomator\venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited normally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location C:\Program Files\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Stacktrace:
Backtrace:
        (No symbol) [0x005B37D3]
        (No symbol) [0x00548B81]
        (No symbol) [0x0044B36D]
        (No symbol) [0x0046B7EF]
        (No symbol) [0x00466FC9]
        (No symbol) [0x004A1ED5]
        (No symbol) [0x004A1B2C]
        (No symbol) [0x0049B216]
        (No symbol) [0x00470D97]
        (No symbol) [0x0047253D]
        GetHandleVerifier [0x0082ABF2+2510930]
        GetHandleVerifier [0x00858EC1+2700065]
        GetHandleVerifier [0x0085C86C+2714828]
        GetHandleVerifier [0x00663480+645344]
        (No symbol) [0x00550FD2]
        (No symbol) [0x00556C68]
        (No symbol) [0x00556D4B]
        (No symbol) [0x00560D6B]
        BaseThreadInitThunk [0x76B27D69+25]
        RtlInitializeExceptionChain [0x77AFB74B+107]
        RtlClearBits [0x77AFB6CF+191]

还有一件事要提一下,我把我的Python版本从3.9升级到了3.11,也就是最新的版本,我希望所有这些信息都能帮助我们追踪错误。
使用:

Python3.11, selenium==4.8.2, webdriver-manager==3.8.5

更多信息chrome://version显示配置文件路径如下:

Profile Path:   C:\Users\abdul\AppData\Local\Google\Chrome\User Data\Default

编辑:看起来Chrome驱动程序和Chrome的版本不匹配**chrome driver site上唯一可用的驱动程序是:***

我的Chrome版本

qlzsbp2j

qlzsbp2j1#

您必须从chrome_userdata_dir中删除配置文件目录(即Default)。此外,您不能使用默认配置文件,但您必须创建一个新的配置文件(here您可以找到一个关于如何在Chrome中创建用户配置文件的分步教程)。然后运行以下命令

options.add_argument("user-data-dir=C:\\Users\\gtu\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument("profile-directory=Profile 3")

相关问题