为什么 selenium 无法打开我的 chrome 个人资料?

Why selenium wont open my chrome profile?

我的 selenium 驱动程序无法打开我的 chrome 配置文件。 Chrome 驱动程序与我的 chrome 版本相同。它只是打开干净的浏览器。

代码如下:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

options = Options()
browser = webdriver.Chrome(executable_path="C:/Users/rektl/Desktop/csdd projekts/chromedriver.exe", chrome_options=options)
options.add_argument("user-data-dir=C:/Users/Rektl/AppData/Local/Google/Chrome/User Data/Profile 2")

browser.get("https://google.com")

一些错误代码如果有帮助的话:

[8052:3876:0303/141810.953:ERROR:device_event_log_impl.cc(214)] [14:18:10.953] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[8052:3876:0303/141810.965:ERROR:device_event_log_impl.cc(214)] [14:18:10.965] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[8052:3988:0303/141810.995:ERROR:chrome_browser_main_extra_parts_metrics.cc(239)] END: GetDefaultBrowser()

您定义了 optins 但仅在创建 Web 驱动程序实例后才在其中添加了值,因此这不会有任何效果。
你应该把行

options.add_argument("user-data-dir=C:/Users/Rektl/AppData/Local/Google/Chrome/User Data/Profile 2")

之前

browser = webdriver.Chrome(executable_path="C:/Users/rektl/Desktop/csdd projekts/chromedriver.exe", chrome_options=options)

请试试这个:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

options = Options()
options.add_argument("user-data-dir=C:/Users/Rektl/AppData/Local/Google/Chrome/User Data/Profile 2")
browser = webdriver.Chrome(executable_path="C:/Users/rektl/Desktop/csdd projekts/chromedriver.exe", chrome_options=options)

browser.get("https://google.com")

试试这个:从 chrome://version/

复制配置文件路径

例如C:\Users\pcuser\AppData\Local\Google\Chrome\UserData\Default

        opt = webdriver.ChromeOptions()
        opt.binary_location = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'    
        chromedriver_exe_location = os.path.join(os.getcwd(), 'chromedriver.exe')  
        profile_path = r'C:\Users\pcuser\AppData\Local\Google\Chrome\User Data' # path minus last folder
        opt.add_argument('--user-data-dir={}'.format(profile_path))
        opt.add_argument('--profile-directory={}'.format('DEFAULT')) # last folder name
        driver = webdriver.Chrome(chromedriver_exe_location, options=opt, service_args='')