[英]How to load custom profile in Chrome using Python Selenium Webdriver?
[英]How to load default profile in Chrome using Python Selenium Webdriver?
我想使用 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 将为它创建新的配置文件和目录。
这解决了我的问题。 (最后去掉默认)
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
只是为了分享对我有用的东西。 使用默认配置文件很复杂,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
并将所有数据保存在我想要的地方,并且很容易将您的项目移动到另一台机器上。
这个答案非常简单且不言自明。
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://stackoverflow.com/a/57894065/4061346")
正如@MadRabbit 所说,在地址栏中输入chrome://version/
以查找您的 chrome 配置文件数据的路径。
C:\\Users\\pc\\AppData\\Local\\Google\\Chrome\\User Data\\Default
看起来像这样/Users/user/Library/Application Support/Google/Chrome/Default
看起来像这样因此,您所要做的就是从配置文件路径中删除
的最后一部分。Default
注意:确保不要同时运行多个会话以避免出现问题。
我用“Yoannes Geissler”的回答解决了我的问题。
就我而言,我的个人资料被命名为“个人资料 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')
我想使用Python的网络驱动程序以默认配置文件启动Chrome,以使Cookie和网站偏好设置在各个会话中均保持不变。
我怎样才能做到这一点?
这就是我所做的。
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驱动路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.