我想使用Python的webdriver启动Chrome浏览器的默认配置文件,以便在会话间保留cookies和站点偏好设置。
我该怎么做?
我该怎么做?
这是最终让它为我工作的方法。
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 将为其创建新的配置文件和目录。
这解决了我的问题。(在结尾处删除“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我用“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 Deefrom 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配置文件数据路径。
C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default
/Users/user/Library/Application Support/Google/Chrome/Default
因此,您只需要从配置文件路径中删除最后一部分。Default
注意:确保不要同时运行多个会话以避免出现问题。
Default
是什么意思? - Luk Aron我来分享一下我成功解决问题的方法。使用默认配置文件会比较复杂,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/")
User Data
并将所有数据保存在其中,这样我就可以方便地将项目移动到另一台机器上。这是我所做的。
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驱动程序的路径。
/Default/
。但现在我遇到了问题,当添加cookies时,Chrome实例打开,该实例已经登录我的谷歌帐户,但它失败并出现异常,无法工作。 - Usama Tahir