简体   繁体   English

如何通过 Python 打开 Chrome 配置文件

[英]How to open a Chrome Profile through Python

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.我刚刚添加了该选项,因此它将使用此代码在 chrome 上打开一个配置文件。

options = webdriver.ChromeOptions
browser = webdriver.Chrome(executable_path=r"C:\Users\princess\AppData\Local\Programs\Python\Python36-32\chromedriver.exe", chrome_options=options)
options.add_argument(r'user-data-dir=C:\Users\princess\AppData\Local\Google\Chrome\User Data')
options.add_argument('--profile-directory=Profile 1')

When used, I get this error code.使用时,我收到此错误代码。

C:\Users\Princess\Desktop>CHBO.py
Traceback (most recent call last):
  File "C:\Users\Princess\Desktop\CHBO.py", line 12, in <module>
    browser = webdriver.Chrome(executable_path=r"C:\Users\princess\AppData\Local\Programs\Python\Python36-32\chromedriver.exe", chrome_options=options)
  File "C:\Users\Princess\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 59, in __init__
    desired_capabilities = options.to_capabilities()
TypeError: to_capabilities() missing 1 required positional argument: 'self'

How can I fix this?我该如何解决这个问题?

To create and open a new Chrome Profile you need to follow the following steps :创建打开新的Chrome 配置文件,您需要按照以下步骤操作:

  • Open Chrome browser, click on the Side Menu and click on Settings on which the url chrome://settings/ opens up.打开Chrome浏览器,点击侧边菜单上,并点击该URL设置chrome://settings/打开。
  • 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 的快照

硒

  • Now a desktop icon will be created as SeLeNiUm - Chrome现在桌面图标将创建为SeLeNiUm - Chrome
  • From the properties of the desktop icon SeLeNiUm - Chrome get the name of the profile directory.从桌面图标SeLeNiUm - Chrome的属性中获取配置文件目录的名称。 eg --profile-directory="Profile 2"例如--profile-directory="配置文件 2"

配置文件目录

  • Get the absolute path of the profile-directory in your system as follows :获取系统中配置文件目录的绝对路径,如下所示:

     C:\\\\Users\\\\Otaku_Wiz\\\\AppData\\\\Local\\\\Google\\\\Chrome\\\\User Data\\\\Profile 2
  • Now pass the value of profile-directory through an instance of Options with add_argument() method along with key user-data-dir as follows :现在通过带有add_argument()方法和 key user-data-dirOptions实例传递profile-directory的值,如下所示:

     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")
  • Execute your Test执行你的Test

  • Observe Chrome gets initialized with the Chrome Profile as SeLeNiUm观察Chrome使用Chrome 配置文件初始化为SeLeNiUm

硒

You can use options = Options() or options = webdriver.ChromeOptions() at place of options = webdriver.ChromeOptions您可以使用options = Options()options = webdriver.ChromeOptions()代替options = webdriver.ChromeOptions

Otherwise you are pointing at an object (namely webdriver.ChromeOptions ), and not making an instance of that object by including the needed parenthesis否则,您将指向一个对象(即webdriver.ChromeOptions ),而不是通过包含所需的括号来创建该对象的实例

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

相关问题 如何使用Python Selenium Webdriver通过默认的Chrome配置文件打开URL - How to open URL through default Chrome profile using Python Selenium Webdriver 在已经登录Google的python中打开Chrome配置文件 - Open a chrome profile in python that is already signed into google 如何在python的自定义配置文件中使用硒打开Goog​​le chrome [操作系统:Ubuntu] - How to open Google chrome using selenium in a custom profile in python [Operating System: Ubuntu] 尝试在 python 3.8 中使用 selenium 在我的个人资料上打开 chrome - Trying to open chrome on my profile with selenium in python 3.8 使用mac上的python和selenium打开使用默认用户配置文件的chrome - Open chrome with default user profile using python and selenium on mac Chrome 无法使用 selenium python 打开配置文件 - Chrome can't open a profile using selenium python 如何在Selenium中为Python使用预定义的Chrome配置文件? - How to use predefined chrome profile in selenium for python? Selenium python - 如何加载默认 Chrome 配置文件 - Selenium python - How to load Default Chrome Profile 如何在 Selenium Webdriver Python 3 中使用 Chrome 配置文件 - How to use Chrome Profile in Selenium Webdriver Python 3 使用 selenium 打开多个镀铬轮廓 - open multiple chrome profile with selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM