如何使用 Python Selenium Webdriver 在 Chrome 中加载默认配置文件? [英] How to load default profile in Chrome using Python Selenium Webdriver?

查看:156
本文介绍了如何使用 Python Selenium Webdriver 在 Chrome 中加载默认配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Python 的网络驱动程序以其默认配置文件启动 Chrome,以便 cookie 和站点首选项在会话中保持不变.

I'd like to launch Chrome with its default profile using Python's webdriver so that cookies and site preferences persist across sessions.

我该怎么做?

推荐答案

这就是它最终为我工作的原因.

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)

要查找 chrome 配置文件数据的路径,您需要在地址栏中输入 chrome://version/ .例如.我的显示为 C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default,要在脚本中使用它,我必须排除 \Default\ 所以我们最终只有 C:\Users\pc\AppData\Local\Google\Chrome\User Data.

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.

此外,如果您想为 selenium 设置单独的配置文件:将路径替换为任何其他路径,如果在启动时它不存在,chrome 将为它创建新的配置文件和目录.

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.

这篇关于如何使用 Python Selenium Webdriver 在 Chrome 中加载默认配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆