如何使用Python Selenium Webdriver加载Chrome的默认配置文件?

71
我想使用Python的webdriver启动Chrome浏览器的默认配置文件,以便在会话间保留cookies和站点偏好设置。
我该怎么做?
6个回答

125

这是最终让它为我工作的方法。

from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)

要找到 Chrome 配置文件数据的路径,您需要在地址栏中输入 chrome://version/。例如,我的显示为 C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default,为了在脚本中使用它,我需要排除 \Default\,这样我们最终只得到 C:\Users\pc\AppData\Local\Google\Chrome\User Data

如果您想为 Selenium 设置单独的配置文件,请将路径更改为任何其他路径,并且如果在启动 Chrome 时该路径不存在,则 Chrome 将为其创建新的配置文件和目录。


9
谢谢,我花了几个小时找不到这个问题的答案,最终删除路径中的默认设置就解决了。 - user3281831
2
这只在我的 Chrome 关闭时才能正常工作。必须通过创建配置文件目录的副本并提供其路径来修复此问题,而不是使用原始路径。否则,如果另一个 Chrome 已经使用相同的配置文件打开,则会出现目录被占用的情况。 - anandhu
2
最终成功从路径中删除了/Default/。但现在我遇到了问题,当添加cookies时,Chrome实例打开,该实例已经登录我的谷歌帐户,但它失败并出现异常,无法工作。 - Usama Tahir
获取默认用户配置文件的 Firefox 版本是什么? - fractal
使用后,我的Chrome总是在隐身模式下打开,并且我的Google账户也丢失了?请问如何恢复? - Skittles
显示剩余4条评论

19

这解决了我的问题。(在结尾处删除“Default”)

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=/home/username/.config/google-chrome")

cls.driver = webdriver.Chrome(options=options,
                              executable_path="./../ext/chromedriver")

Chrome_Options 已被弃用,请改用 options


你的第一行代码缺少一些内容:from selenium import [...?] - Jundiaius
1
我尝试了这个方法,但对我没有用。@NilsZenker,您能否请看一下我的问题,链接为https://stackoverflow.com/questions/53589103/selenium-chrome-driver-cant-set-user-data-dir?noredirect=1#comment94043600_53589103? - etayluz
我遇到了类似的问题并按照您的指示操作,但是收到了错误消息:selenium.common.exceptions.WebDriverException: Message: Service /Applications/Google Chrome.app/Contents/MacOS/Google Chrome 意外退出。状态代码为:0。 - Victor

17

我用“Yoannes Geissler”的答案解决了我的问题。

在我这种情况下,我的个人资料命名为“Profile 2”。

我的代码:

options = webdriver.ChromeOptions() 

options.add_argument('--user-data-dir=C:/Users/GOD/AppData/Local/Google/Chrome/User Data')

options.add_argument('--profile-directory=Profile 2')

wd = webdriver.Chrome(options=options)

以下这行解决了我的问题:

options.add_argument('--profile-directory=Profile 2')

没有加上这一行 options.add_argument('--profile-directory=Profile 2') 对我来说也可以工作。不过,我猜对于有些人来说可能会不同。或者是因为你有多个配置文件。 - Ecks Dee
我确认这个在我的Macbook Mojave Pycharm上非常好用和合乎逻辑... 机制就是这么简单:
  • 第一行(user-data-dir)是'Chrome'大文件夹的目录
  • 第二行(profile-directory)是你的个人资料名称。 我认为Selenium官方在某处有明确说明,但是在这篇帖子中没有人找到。
- Zui Zui

5
这个答案很简单并且自我解释。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

exec_path_chrome = "path/to/Google Chrome" #Do not use this path that is extracted from "chrome://version/"
exec_path_driver = "path/to/chromedriver"

ch_options = Options() #Chrome Options
ch_options.add_argument("user-data-dir = /path/to/Chrome Profile") #Extract this path from "chrome://version/"

driver = webdriver.Chrome(executable_path = exec_path_driver, options = ch_options) #Chrome_Options is deprecated. So we use options instead.

driver.get("https://dev59.com/Ml0Z5IYBdhLWcg3wyy11#57894065")

如@MadRabbit所说,输入 chrome://version/ 到地址栏中可以找到您的Chrome配置文件数据路径。

  • 在Windows中会显示为 C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default
  • 在Mac中会显示为 /Users/user/Library/Application Support/Google/Chrome/Default

因此,您只需要从配置文件路径中删除最后一部分Default

注意:确保不要同时运行多个会话以避免出现问题。


我遇到了类似的问题并按照您的指示操作,但是收到了错误消息:selenium.common.exceptions.WebDriverException: Message: Service /Applications/Google Chrome.app/Contents/MacOS/Google Chrome 意外退出。状态代码为:0。 - Victor
@Victor,请查看以下链接 https://dev59.com/1Zvga4cB1Zd3GeqP344F#39998437 和 https://dev59.com/fFYO5IYBdhLWcg3wTf1v#46027522 并告诉我哪一个解决了你的问题。 - Joe
嗨Youssof H,非常感谢您回复我,但是我按照您的建议添加了Chrome二进制文件的路径,但是配置文件仍然无法加载。我有以下两个问题,更详细地描述了我的问题,您介意看一下吗?https://stackoverflow.com/questions/60048743/selenium-with-firefor-or-chrome-profile和https://stackoverflow.com/questions/60031070/webdriverexception-error-when-using-selenium-chrome-webdriver-with-options - Victor
你的意思是从配置路径中删除最后一部分 Default 是什么意思? - Luk Aron

5

我来分享一下我成功解决问题的方法。使用默认配置文件会比较复杂,Chrome 经常崩溃。

from pathlib import Path
from selenium import webdriver

driver_path = Path("{}/driver/chromedriver75.exe".format(PATH_TO_FOLDER))
user_data_dir = Path("{}/driver/User Data".format(PATH_TO_FOLDER))

options = webdriver.ChromeOptions()

# TELL WHERE IS THE DATA DIR
options.add_argument("--user-data-dir={}".format(user_data_dir))

# USE THIS IF YOU NEED TO HAVE MULTIPLE PROFILES
options.add_argument('--profile-directory=Default')

driver = webdriver.Chrome(executable_path=driver_path, options=options)

driver.get("https://google.com/")

通过这样做,Chrome会创建文件夹User Data并将所有数据保存在其中,这样我就可以方便地将项目移动到另一台机器上。

我有一个类似的问题,按照您的指示操作,但是收到了错误消息:selenium.common.exceptions.WebDriverException: Message: Service /Applications/Google Chrome.app/Contents/MacOS/Google Chrome 意外退出。状态代码为:0。 - Victor

1

这是我所做的。

import chromedriver_binary
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument(r"--user-data-dir=User Data Directory")
chrome_options.add_argument(r"--profile-directory=Profile name")
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches",["enable 
logging"])
chrome_options.add_experimental_option("excludeSwitches", ["enable 
automation"])
chrome_options.add_argument("start-maximized")
self.driver = webdriver.Chrome(chrome_options=chrome_options)
self.wait = WebDriverWait(self.driver, 20)

在这里我们不需要提供Chrome驱动程序的路径。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接