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

如何在Selenium Webdriver Python 3中使用Chrome配置文件,python,selenium,google-chrome,selenium-chromedriver,chrome-profile,Python,Selenium,Google Chrome,Selenium Chromedriver,Chrome Profile,因此,每当我尝试使用我的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 err

因此,每当我尝试使用我的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

在我的狂欢节上。我不知道这意味着什么,我很高兴能得到任何帮助。提前谢谢

确保配置文件的路径正确,并且在所述路径中加倍转义反斜杠

例如,windows上的默认配置文件通常位于:


“C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\user Data\\Default”

确保配置文件的路径正确,并且在所述路径中重复转义反斜杠

例如,windows上的默认配置文件通常位于:


“C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\user Data\\Default”

您确定要将webdriver路径放入user Data dir参数中吗?这通常是您放置chrome配置文件的位置,例如“C:\Users\yourusername\AppData\Local\Google\chrome\User Data\profile 1\”。此外,您还需要在目录路径中使用双反斜杠或正斜杠(两者都有效)。您可以使用操作系统库测试路径是否有效 e、 g

我会给你目录清单

我还可能补充说,如果您在使用指定的用户配置文件运行webdriver时设法使chrome崩溃,它似乎会在配置文件中记录崩溃,下次打开chrome时,您会在chrome异常退出后收到恢复页面的chrome提示。对我个人来说,这是一个有点头疼的问题,我不再使用chromedriver的用户配置文件,因为它。我找不到绕过它的办法。其他人在这里报告过,但他们的解决方案似乎都不适合我,或者不适合我的测试用例。
如果未指定用户配置文件,则每次运行时都会创建一个新的(空白)临时配置文件

是否确定要将webdriver路径放入user data dir参数中?这通常是您放置chrome配置文件的位置,例如“C:\Users\yourusername\AppData\Local\Google\chrome\User Data\profile 1\”。此外,您还需要在目录路径中使用双反斜杠或正斜杠(两者都有效)。您可以使用操作系统库测试路径是否有效 e、 g

我会给你目录清单

我还可能补充说,如果您在使用指定的用户配置文件运行webdriver时设法使chrome崩溃,它似乎会在配置文件中记录崩溃,下次打开chrome时,您会在chrome异常退出后收到恢复页面的chrome提示。对我个人来说,这是一个有点头疼的问题,我不再使用chromedriver的用户配置文件,因为它。我找不到绕过它的办法。其他人在这里报告过,但他们的解决方案似乎都不适合我,或者不适合我的测试用例。
如果您没有指定用户配置文件,则每次运行时都会根据您的问题和代码试用创建一个新的(空白)临时配置文件。如果您想打开Chrome浏览会话,请选择以下选项:

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

  • 注意:您的默认chrome配置文件将包含大量书签、扩展、主题、cookies等。Selenium可能无法加载它。因此,根据最佳实践,为@Test创建一个新的chrome配置文件,并在配置文件中存储/保存/配置所需的数据

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

  • 在这里,您将看到有关的详细讨论


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

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

  • 注意:您的默认chrome配置文件将包含大量书签、扩展、主题、cookies等。Selenium可能无法加载它。因此,根据最佳实践,为@Test创建一个新的chrome配置文件,并在配置文件中存储/保存/配置所需的数据

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

  • 在这里,您将看到有关的详细讨论


要获取路径,请执行以下步骤

在搜索栏中键入以下内容并按enter键

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


要获取路径,请执行以下步骤

在搜索栏中键入以下内容并按enter键

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


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

<?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);

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

<?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);

公认的答案是错误的。这是一种正式而正确的方法:

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配置文件的桌面快捷方式,然后转到属性->快捷方式,您将在“目标”文本框中找到它。

接受的答案是错误的。这是一种正式而正确的方法:

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配置文件的桌面快捷方式,然后转到属性->快捷方式,您将在“目标”文本框中找到它。

我可以加载默认配置文件。但是,一旦我添加了配置文件2(我已经创建了它,现在它位于用户数据文件夹中),它将不会加载配置文件。我可以加载默认的pro
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")