如何通过 Python 打开 Chrome 配置文件 [英] How to open a Chrome Profile through Python
本文介绍了如何通过 Python 打开 Chrome 配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在编写的脚本运行良好.我刚刚添加了该选项,以便使用此代码在 chrome 上打开一个配置文件.
My script I have been writing has been working great. I just added the option so it would open a profile on chrome using this code.
options = webdriver.ChromeOptions
browser = webdriver.Chrome(executable_path=r"C:UsersprincessAppDataLocalProgramsPythonPython36-32chromedriver.exe", chrome_options=options)
options.add_argument(r'user-data-dir=C:UsersprincessAppDataLocalGoogleChromeUser Data')
options.add_argument('--profile-directory=Profile 1')
使用时,我收到此错误代码.
When used, I get this error code.
C:UsersPrincessDesktop>CHBO.py
Traceback (most recent call last):
File "C:UsersPrincessDesktopCHBO.py", line 12, in <module>
browser = webdriver.Chrome(executable_path=r"C:UsersprincessAppDataLocalProgramsPythonPython36-32chromedriver.exe", chrome_options=options)
File "C:UsersPrincessAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriverchromewebdriver.py", line 59, in __init__
desired_capabilities = options.to_capabilities()
TypeError: to_capabilities() missing 1 required positional argument: 'self'
我该如何解决这个问题?
How can I fix this?
推荐答案
要创建和打开一个新的Chrome个人资料,您需要遵循以下步骤:
To create and open a new Chrome Profile you need to follow the following steps :
- 打开Chrome浏览器,点击侧边菜单并点击设置,url
chrome://settings/
打开. - 在人员部分,点击弹出窗口的管理其他人员.
- 点击ADD PERSON,提供人名,选择一个图标,保留该项目为此创建一个桌面快捷方式用户选中并点击ADD按钮.
- 您的新个人资料已创建.
- 新个人资料的快照SeLeNiUm
- Open Chrome browser, click on the Side Menu and click on Settings on which the url
chrome://settings/
opens up. - In People section, click on Manage other people on which a popup comes up.
- Click on ADD PERSON, provide the person name, select an icon, keep the item Create a desktop shortcut for this user checked and click on ADD button.
- Your new profile gets created.
- Snapshot of a new profile SeLeNiUm
- 现在桌面图标将创建为 SeLeNiUm - Chrome
- 从桌面图标的属性 SeLeNiUm - Chrome 中获取配置文件目录的名称.例如--profile-directory="Profile 2"
- Now a desktop icon will be created as SeLeNiUm - Chrome
- From the properties of the desktop icon SeLeNiUm - Chrome get the name of the profile directory. e.g. --profile-directory="Profile 2"
获取系统中profile-directory的绝对路径如下:
C:\Users\Otaku_Wiz\AppData\Local\Google\Chrome\User Data\Profile 2
现在将 profile-directory 的值通过 Options 的实例和 add_argument()
方法以及键 传递>user-data-dir 如下:
Now pass the value of profile-directory through an instance of Options with add_argument()
method along with key user-data-dir as follows :
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 ochromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")
执行你的Test
这篇关于如何通过 Python 打开 Chrome 配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文