繁体   English   中英

在Python中使用Selenium的默认Chrome配置文件

[英]Use default Chrome profile with Selenium in Python

当我像这样用Selenium启动Chrome时......

from selenium import webdriver
driver = webdriver.Chrome("/path/to/chromedriver")

...我得到一个没有浏览历史记录的“干净”Chrome实例,而且我的计算机上没有正常安装Chrome的存储数据。

我想使用Selenium打开Chrome,但在打开的浏览器中可以使用默认Chrome安装中的书签,历史记录,Cookie,缓存等。

我怎样才能做到这一点?

您可以使用特定的个人资料或问题中的默认个人资料:

如何在Python中使用Selenium在Chrome中打开一个新的默认浏览器窗口?

这是一个设置的剪辑:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--user-data-dir=THE/PATH/TO/YOUR/PROFILE") # change to profile path
chrome_options.add_argument('--profile-directory=Profile 1')

browser = webdriver.Chrome(executable_path="PATH/TO/cromedriver.exe", chrome_options=chrome_options) # change the executable_path too

要查找配置文件的路径,只需在默认的chrome浏览器中键入chrome://version/ ,您将在Profile Path下看到它在我的PC中它是"C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Default"

希望你觉得这很有帮助!

如果您只是想尝试打开新浏览器,可以使用Selenium创建一个附带Chrome的新驱动程序。 您需要从此处下载chrome驱动程序,并将其放在路径中,如下所示。

这是python代码:

from selenium import webdriver

driver = webdriver.Chrome("path-to-chromedriver")
driver.get("https://www.google.com/")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM