如何使用 Python Selenium Webdriver 在 chrome 中加载默认配置文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31062789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load default profile in chrome using Python Selenium Webdriver?
提问by MadRabbit
So I'd like to open chrome with its default profile using pythons webdriver. I've tried everything I could find but I still couldn't get it to work. Thanks for the help!
所以我想使用pythons webdriver使用其默认配置文件打开chrome。我已经尝试了所有我能找到的方法,但我仍然无法让它工作。谢谢您的帮助!
采纳答案by MadRabbit
This is what finally got it working for me.
这就是最终让它为我工作的原因。
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)
To find path to your chrome profile data you need to type chrome://version/
into address bar . For ex. mine is displayed as C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default
, to use it in the script I had to exclude \Default\
so we end up with only C:\Users\pc\AppData\Local\Google\Chrome\User Data
.
要找到您的 chrome 配置文件数据的路径,您需要在chrome://version/
地址栏中输入。例如。我的显示为C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default
,要在我必须排除的脚本中使用它,\Default\
所以我们最终只有C:\Users\pc\AppData\Local\Google\Chrome\User Data
.
Also if you want to have separate profile just for selenium: replace the path with any other path and if it doesn't exist on start up chrome will create new profile and directory for it.
此外,如果您只想为 selenium 设置单独的配置文件:将路径替换为任何其他路径,如果它在启动时不存在,chrome 将为它创建新的配置文件和目录。
回答by Nils Zenker
This solved my problem. (remove Default at the end)
这解决了我的问题。(最后去掉默认)
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
ist deprecated. Use options
instead
Chrome_Options
已弃用。使用options
替代
回答by Yoannes Geissler
Just to share what worked for me. Using default's profile was complicated, chrome keeps crashing.
只是为了分享对我有用的东西。使用默认配置文件很复杂,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/")
By doing this Chrome will create the folder User Data
and keep all the data in it where I want and it's easy to just move your project to another machine.
通过这样做,Chrome 将创建文件夹User Data
并将所有数据保存在我想要的位置,并且很容易将您的项目移动到另一台机器上。
回答by Youssof H.
This answer is pretty simple and self-explained.
这个答案非常简单且不言自明。
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")
As @MadRabbit said type chrome://version/
into the address bar to find the path to your chrome profile data.
正如@MadRabbit 所说,chrome://version/
在地址栏中键入以查找您的 chrome 配置文件数据的路径。
- It appears like this in Windows
C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default
- It appears like this in Mac
/Users/user/Library/Application Support/Google/Chrome/Default
- 在 Windows 中看起来像这样
C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default
- 在 Mac 中看起来像这样
/Users/user/Library/Application Support/Google/Chrome/Default
So all you have to do is to delete the last portion from the profile path.Default
所以你所要做的就是从配置文件路径中删除最后一部分。Default
Note:Make sure you don't run more than one session at the same time to avoid problems.
注意:确保不要同时运行多个会话以避免出现问题。