简体   繁体   English

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

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

So whenever I try to use my Chrome settings (the settings I use in the default browser) by adding因此,每当我尝试通过添加来使用我的 Chrome 设置(我在默认浏览器中使用的设置)时

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\Users\... (my webdriver path)")
driver = webdriver.Chrome(executable_path="myPath", options=options)

it shows me the error code它显示了错误代码

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

in my bash.在我的狂欢中。 I don't know what that means and I'd be happy for any kind of help I can get.我不知道这意味着什么,我会很高兴能得到任何帮助。 Thanks in advance!提前致谢!

The accepted answer is wrong.接受的答案是错误的。 This is the official and correct way to do it:这是官方和正确的方法:

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")

To find the profile folder on Windows right-click the desktop shortcut of the Chrome profile you want to use and go to properties -> shortcut and you will find it in the "target" text box.要在 Windows 上找到配置文件文件夹,请右键单击要使用的 Chrome 配置文件的桌面快捷方式,然后转到属性 -> 快捷方式,您将在“目标”文本框中找到它。

As per your question and your code trials if you want to open a Chrome Browsing Session here are the following options:根据您的问题和代码试验,如果您想打开Chrome 浏览会话,这里有以下选项:

  • To use the default Chrome Profile :要使用默认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")
  • Note: Your default chrome profile would contain a lot of bookmarks, extensions, theme, cookies etc. Selenium may fail to load it.注意:您的默认 chrome 配置文件将包含许多书签、扩展程序、主题、cookie 等。Selenium可能无法加载它。 So as per the best practices create a new chrome profile for your @Test and store/save/configure within the profile the required data.因此,按照最佳实践,为您的@Test创建一个新的chrome 配置文件,并在配置文件中存储/保存/配置所需的数据。

  • To use the customized Chrome Profile :要使用自定义的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")
  • Here you will find a detailed discussion on How to open a Chrome Profile through Python在这里,您将找到有关如何通过 Python 打开 Chrome 配置文件的详细讨论

To get the path, follow the steps below.要获取路径,请按照以下步骤操作。

In the search bar type the following and press enter在搜索栏中输入以下内容并按回车

在此处输入图像描述

This will then show all the metadata.这将显示所有元数据。 There find the path to the profile在那里找到配置文件的路径

在此处输入图像描述

Are you sure you are meant to be putting in the webdriver path in the user-data-dir argument?您确定要在 user-data-dir 参数中放入 webdriver 路径吗? That's usually where you put your chrome profile eg "C:\Users\yourusername\AppData\Local\Google\Chrome\User Data\Profile 1\".这通常是您放置 chrome 配置文件的位置,例如“C:\Users\yourusername\AppData\Local\Google\Chrome\User Data\Profile 1\”。 Also you will need to use either double backslashes or forward slashes in your directory path (both work).此外,您还需要在目录路径中使用双反斜杠或正斜杠(两者都有效)。 You can test if your path works by using the os library eg您可以使用 os 库来测试您的路径是否有效,例如

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

will give you the directory listing.会给你目录列表。

I might also add that occasionally if you manage to crash chrome while running webdriver with a nominated user profile, that it seems to record the crash in the profile and the next time you open chrome, you get the Chrome prompt to restore pages after it exited abnormally.我还可以偶尔补充一点,如果您在使用指定的用户配置文件运行 webdriver 时设法使 chrome 崩溃,它似乎会在配置文件中记录崩溃,并且下次打开 chrome 时,您会在退出后收到 Chrome 提示以恢复页面异常。 For me personally this had been a bit of headache to deal with and I no longer use a user profile with chromedriver because of it.就我个人而言,这有点让人头疼,因此我不再使用带有 chromedriver 的用户配置文件。 I could not find a way around it.我找不到解决办法。 Other people have reported it here, but none of their solutions seemed to work for me, or were not suitable for my test cases.其他人在这里报告了它,但他们的解决方案似乎都不适合我,或者不适合我的测试用例。 https://superuser.com/questions/237608/how-to-hide-chrome-warning-after-crash If you don't nominate a user profile it seems to create a new (blank) temporary one each time it runs https://superuser.com/questions/237608/how-to-hide-chrome-warning-after-crash如果您不指定用户配置文件,它似乎会在每次运行时创建一个新的(空白)临时文件

This is how I managed to use EXISTING CHROME PROFILE in php selenium webdriver .这就是我设法在php selenium webdriver中使用 EXISTING CHROME PROFILE 的方式。 Profile 6 is NOT my default profile.配置文件 6 不是我的默认配置文件。 I dont know how to run default profile.我不知道如何运行默认配置文件。 It is IMPORTANT not to add -- before chrome option arguments!重要的是不要在 chrome 选项参数之前添加 -- ! All other variants of options didnt work!所有其他选项的变体都不起作用!

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

To get name of your chrome profile, go to chrome://settings/manageProfile, click on profile icon, click "Show profile shortcut on my desktop".要获取您的 chrome 配置文件的名称,请转到 chrome://settings/manageProfile,单击配置文件图标,单击“在我的桌面上显示配置文件快捷方式”。 After that right click on desktop profile icon and go to properties, here you will see something like "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory= "Profile 6" .之后右键单击桌面配置文件图标并转到属性,在这里您将看到类似 "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory= "Profile 6"的内容。

Also I recommend you to close all chrome instances before running this code.此外,我建议您在运行此代码之前关闭所有 chrome 实例。 Also maybe you need to TURN OFF chrome settings > advanced > system > "Continue running background apps when Google Chrome is closed".此外,也许您需要关闭 chrome 设置 > 高级 > 系统 > “在 Google Chrome 关闭时继续运行后台应用程序”。

None of the given answers were working for me so I researched a bit and now the working code is for is this one.给定的答案都不适合我,所以我进行了一些研究,现在工作代码就是这个。 I copied the user dir folder from Profile Path from chrome://version/ and made another argument for the profile as shown below:我从 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')

Make sure you've got the path to the profile right, and that you double escape backslashes in said path.确保您的配置文件路径正确,并且在所述路径中使用双转义反斜杠。

For example, typically the default profile on windows is located at:例如,通常 Windows 上的默认配置文件位于:

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

I managed to launch my chrome profile using these arguments:我设法使用这些参数启动了我的 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);

You can find out more about the web driver here您可以在此处找到有关 Web 驱动程序的更多信息

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

  1. Get profile name by navigating to chrome://version from your chrome browser (You'll see Profile Path, but you only want the profile name from it (eg Profile 1)通过从 chrome 浏览器导航到 chrome://version 获取配置文件名称(您会看到配置文件路径,但您只需要其中的配置文件名称(例如配置文件 1)
  2. Close out all Chrome sessions using the profile you want to use.使用您要使用的配置文件关闭所有 Chrome 会话。 (or else you will get the following error: InvalidArgumentException) (否则您将收到以下错误:InvalidArgumentException)
  3. Now make sure you have the code below (Make sure you replace UserFolder with the name of your userfolder.现在确保您有下面的代码(确保将 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)

this worked for me 100% and it showed up my selected profile.这对我来说 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.

相关问题 如何在python中将现有的Google Chrome配置文件与Selenium Chrome Webdriver一起使用? - How to use an existing google chrome profile with selenium chrome webdriver in python? 如何使用 Python Selenium Webdriver 在 Chrome 中加载默认配置文件? - How to load default profile in Chrome using Python Selenium Webdriver? 如何使用 Python Selenium Webdriver 在 Chrome 中加载自定义配置文件? - How to load custom profile in Chrome using Python Selenium Webdriver? 如何获取 chrome webdriver selenium 路径到 python 中的配置文件? - How to get chrome webdriver selenium path to profile files in python? 如何在Selenium中为Python使用预定义的Chrome配置文件? - How to use predefined chrome profile in selenium for python? 如何在selenium中使用chrome webdriver下载python中的文件? - How to use chrome webdriver in selenium to download files in python? [Python] [Selenium]无法在Chrome中使用网络驱动程序 - [Python][Selenium]Unable to use webdriver with Chrome 如何使用Python Selenium Webdriver通过默认的Chrome配置文件打开URL - How to open URL through default Chrome profile using Python Selenium Webdriver 在Python中使用Selenium的默认Chrome配置文件 - Use default Chrome profile with Selenium in Python Python Selenium 使用用户个人资料 Chrome - Python Selenium Use User Profile Chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM