首页  >  文章  >  后端开发  >  如何使用 Python Selenium Webdriver 在 Chrome 中加载默认配置文件?

如何使用 Python Selenium Webdriver 在 Chrome 中加载默认配置文件?

Barbara Streisand
Barbara Streisand原创
2024-10-27 10:24:02841浏览

How to Load the Default Profile in Chrome with Python Selenium Webdriver?

使用 Python Selenium Webdriver 在 Chrome 中加载默认配置文件

为了在 Chrome 中跨会话保留 cookie 和站点首选项,有必要使用默认配置文件启动浏览器。

解决方案:

利用 webdriver.ChromeOptions 类,使用 add_argument 方法指定默认配置文件的路径:

<code class="python">options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\Path")</code>

将“C:Path”替换为 Chrome 个人资料数据的实际路径。要确定此路径,请导航至地址栏中的 chrome://version。例如,如果您的路径显示为“C:UserspcAppDataLocalGoogleChromeUser DataDefault”,则仅包含直至“User Data”的路径:

<code class="python">options.add_argument("user-data-dir=C:\Users\pc\AppData\Local\Google\Chrome\User Data")</code>

最后,使用指定选项创建一个 webdriver.Chrome 实例:

<code class="python">w = webdriver.Chrome(executable_path="C:\Users\chromedriver.exe", chrome_options=options)</code>

以上是如何使用 Python Selenium Webdriver 在 Chrome 中加载默认配置文件?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn