在Python中使用默认的firefox配置文件和selenium webdriver

问题描述 投票:0回答:3

我知道以前也有人问过类似的问题,但我已经尝试了很多次,但它仍然对我不起作用。

我在 Firefox 中只有一个默认配置文件(称为 c1r3g2wi.default),没有其他配置文件。当我使用 selenium webdriver 启动我的 Firefox 浏览器时,我希望我的 Firefox 浏览器能够从此配置文件启动。我如何在 Python 中执行此操作?

我这样做了:

fp = webdriver.FirefoxProfile('C:\Users\admin\AppData\Roaming\Mozilla\Firefox\Profiles\c1r3g2wi.default')
browser = webdriver.Firefox(fp)

但是我得到了一个错误:

WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 
'C:\\Users\x07dmin\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\c1r3g2wi.default/*.*'

非常感谢帮助或正确方向的指示。

python selenium webdriver
3个回答
9
投票

好吧,我只是通过简单地将文件路径中的所有斜杠从“\”更改为“/”来解决这个问题。 从来不知道这会有所作为。

C:/Users/admin/AppData/Roaming/Mozilla/Firefox/Profiles/c1r3g2wi.default

3
投票

此外,您可以在路径中使用双反斜杠:

fp = webdriver.FirefoxProfile('C:\\Users\\admin\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\c1r3g2wi.default')
browser = webdriver.Firefox(fp)

0
投票

从 Selenium 4.16 开始,使用给定配置文件打开浏览器的正确方法是:

from selenium import webdriver
options = webdriver.FirefoxOptions()
options.profile = webdriver.FirefoxProfile("/path/to/profile")
browser = webdriver.Firefox(options=options)
© www.soinside.com 2019 - 2024. All rights reserved.