如何在 Selenium Webdriver Python 3 中使用 Chrome 配置文件

问题描述 投票:0回答:5

因此,每当我尝试通过添加

来使用我的 Chrome 设置(我在默认浏览器中使用的设置)时
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\Users\... (my webdriver path)")
driver = webdriver.Chrome(executable_path="myPath", options=options)

它向我显示错误代码

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes n 16-17: truncated \UXXXXXXXX escape

在我的狂欢中。我不知道这意味着什么,我很乐意获得任何形式的帮助。预先感谢!

python selenium google-chrome selenium-chromedriver chrome-profile
5个回答
42
投票

接受的答案是错误的。这是官方且正确的方法:

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

options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\path\to\chrome\user\data") #e.g. C:\Users\You\AppData\Local\Google\Chrome\User Data
options.add_argument(r'--profile-directory=YourProfileDir') #e.g. Profile 3
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")

要在 Windows 上查找配置文件文件夹,请右键单击要使用的 Chrome 配置文件的桌面快捷方式,然后转到属性 -> 快捷方式,您将在“目标”文本框中找到它。


30
投票

要获取路径,请按照以下步骤操作。

在搜索栏中输入以下内容并按 Enter

这将显示所有元数据。在那里找到配置文件的路径


22
投票

根据您的问题和您的代码试验,如果您想打开Chrome浏览会话,这里有以下选项:

  • 要使用默认的 Chrome 配置文件

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = webdriver.ChromeOptions()
    options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
    driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
    driver.get("https://www.google.co.in")
    
  • 注意:您的默认 chrome 配置文件将包含大量书签、扩展、主题、cookie 等。Selenium可能无法加载它。因此,根据最佳实践,为您的 @Test 创建一个新的 chrome 配置文件,并在配置文件中存储/保存/配置所需的数据。

  • 要使用自定义的 Chrome 配置文件

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2")
    driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
    driver.get("https://www.google.co.in")
    
  • 在这里您将找到关于如何通过Python打开Chrome配置文件

  • 的详细讨论

7
投票

给出的答案都不适合我,所以我做了一些研究,现在的工作代码就是这个。我从 chrome://version/ 的配置文件路径复制了用户目录文件夹,并为配置文件设置了另一个参数,如下所示:

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

options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=C:\\Users\\gupta\\AppData\\Local\\Google\\Chrome\\User Data')
options.add_argument('profile-directory=Profile 1')
driver = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\chromedriver.exe', options=options)
driver.get('https://google.com')

3
投票

这就是我如何在 php selenium webdriver 中使用现有的 CHROME 配置文件。 配置文件 6 不是我的默认配置文件。我不知道如何运行默认配置文件。重要的是不要在 chrome 选项参数之前添加 -- !所有其他选项的变体都不起作用!

<?php
//...
$chromeOptions = new ChromeOptions();
$chromeOptions->addArguments([
    'user-data-dir=C:/Users/MyUser/AppData/Local/Google/Chrome/User Data',
    'profile-directory=Profile 6'
]);

$host = 'http://localhost:4444/wd/hub'; // this is the default
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $chromeOptions);
$driver = RemoteWebDriver::create($host, $capabilities, 100000, 100000);

要获取您的 chrome 配置文件的名称,请转到 chrome://settings/manageProfile,单击配置文件图标,单击“在我的桌面上显示配置文件快捷方式”。右键单击桌面配置文件图标并转到属性后,您将在此处看到类似“C:\Program Files (x86)\Google\Chrome\Applicatio”的内容

© www.soinside.com 2019 - 2024. All rights reserved.