Python中将默认的Chrome配置文件与Selenium结合

Python中将默认的Chrome配置文件与Selenium结合

本文介绍了在Python中将默认的Chrome配置文件与Selenium结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

...我得到了一个干净"的Chrome实例,该实例没有浏览历史记录,并且也没有从计算机上正常安装的Chrome浏览器保存的数据.

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

我该怎么做?

解决方案

您可以使用特定配置文件或默认配置文件,如您的问题所示:

这是设置的片段:

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/,您就会在我的PC的 Profile Path:下看到它,该文件为"C:\Users\user\AppData\Local\Google\Chrome\User Data\Default"

希望这对您有所帮助!

When I launch Chrome with Selenium like this...

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

... I get a "clean" Chrome instance with no browsing history and none of my stored data from the normal installation of Chrome on my computer.

I want to open Chrome with Selenium but have the bookmarks, history, cookies, cache, etc. from my default Chrome installation available in the opened browser.

How can I do this?

解决方案

You can use a specific profile or the default one as in your question:

Here is a snip of a set up:

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

To find the path to the profile just type chrome://version/ in your default chrome browser and you will see it under Profile Path: in my PC it's "C:\Users\user\AppData\Local\Google\Chrome\User Data\Default"

Hope you find this helpful!

这篇关于在Python中将默认的Chrome配置文件与Selenium结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:39