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

时间:2018-03-14 05:07:27

标签: python selenium google-chrome selenium-chromedriver chrome-profile

我写的剧本一直很好用。我刚添加了该选项,因此它将使用此代码在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')

使用时,我收到此错误代码。

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'

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

创建打开新的 Chrome个人资料,您需要按照以下步骤操作:

  • 打开 Chrome 浏览器,点击侧面菜单,然后点击网址 {{设置 1}}打开。
  • 部分,点击弹出窗口的管理其他人
  • 点击添加人,提供人名,选择图标,保留项目为此创建桌面快捷方式用户选中并单击 ADD 按钮。
  • 您的新个人资料已创建。
  • 新配置文件的快照 SeLeNiUm

SeLeNiUm

  • 现在,桌面图标将创建为 SeLeNiUm - Chrome
  • 从桌面图标 SeLeNiUm - Chrome 的属性中获取配置文件目录的名称。例如 - profile-directory =“个人资料2”

profile-directory

  • 获取系统中 profile-directory 的绝对路径,如下所示:

    chrome://settings/
  • 现在使用C:\\Users\\Otaku_Wiz\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2 方法通过选项的实例传递 profile-directory 的值以及用户数据 - dir 如下:

    add_argument()
  • 执行您的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")

  • 观察 Chrome Chrome配置文件初始化为 SeLeNiUm

SeLeNiUm

答案 1 :(得分:2)

您可以在options = Options()

的位置使用options = webdriver.ChromeOptions()options = webdriver.ChromeOptions