繁体   English   中英

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

[英]How to use Chrome Profile in Selenium Webdriver Python 3

因此,每当我尝试通过添加来使用我的 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

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

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

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

根据您的问题和代码试验,如果您想打开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 配置文件的详细讨论

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

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

在此处输入图像描述

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

在此处输入图像描述

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

import os
os.list("C:\\Users\\yourusername\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1")

会给你目录列表。

我还可以偶尔补充一点,如果您在使用指定的用户配置文件运行 webdriver 时设法使 chrome 崩溃,它似乎会在配置文件中记录崩溃,并且下次打开 chrome 时,您会在退出后收到 Chrome 提示以恢复页面异常。 就我个人而言,这有点让人头疼,因此我不再使用带有 chromedriver 的用户配置文件。 我找不到解决办法。 其他人在这里报告了它,但他们的解决方案似乎都不适合我,或者不适合我的测试用例。 https://superuser.com/questions/237608/how-to-hide-chrome-warning-after-crash如果您不指定用户配置文件,它似乎会在每次运行时创建一个新的(空白)临时文件

这就是我设法在php selenium webdriver中使用 EXISTING CHROME PROFILE 的方式。 配置文件 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\Application\chrome.exe" --profile-directory= "Profile 6"的内容。

此外,我建议您在运行此代码之前关闭所有 chrome 实例。 此外,也许您需要关闭 chrome 设置 > 高级 > 系统 > “在 Google Chrome 关闭时继续运行后台应用程序”。

给定的答案都不适合我,所以我进行了一些研究,现在工作代码就是这个。 我从 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')

确保您的配置文件路径正确,并且在所述路径中使用双转义反斜杠。

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

"C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Default"

我设法使用这些参数启动了我的 chrome 配置文件:

ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data");
options.addArguments("--profile-directory=Profile 2");
WebDriver driver = new ChromeDriver(options);

您可以在此处找到有关 Web 驱动程序的更多信息

您只需将路径中的'\'替换为'/'即可解决。

  1. 通过从 chrome 浏览器导航到 chrome://version 获取配置文件名称(您会看到配置文件路径,但您只需要其中的配置文件名称(例如配置文件 1)
  2. 使用您要使用的配置文件关闭所有 Chrome 会话。 (否则您将收到以下错误:InvalidArgumentException)
  3. 现在确保您有下面的代码(确保将 UserFolder 替换为您的用户文件夹的名称。
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\EnterYourUserFolder\\AppData\\Local\\Google\\Chrome\\User Data") #leave out the profile
options.add_argument("profile-directory=Profile 1") #enter profile here
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe", chrome_options=options)

这对我来说 100% 有效,它显示了我选择的个人资料。

在此处输入图像描述

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


# path of your chrome webdriver
dir_path = os.getcwd()
user_profile_path = os.environ[ 'USERPROFILE' ]

#if "frtkpr" which is ll be your custom profile does not exist it will be created.
option.add_argument( "user-data-dir=" + user_profile_path + "/AppData/Local/Google/Chrome/User Data/frtkpr" )

driver = webdriver.Chrome( dir_path + "/chromedriver.exe",chrome_options=option )
baseUrl = "https://www.facebook.com/"
driver.maximize_window()
driver.get( baseUrl )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM