首页 > 解决方案 > 如何让 Selenium 直接在现有的 Firefox 配置文件上运行?

问题描述

我发现即使我指定:

webdriver.Firefox(
    firefox_profile=webdriver.FirefoxProfile("/my/profile/path")
)

Selenium 实际上会克隆该配置文件并基于该临时克隆启动浏览器。当您使用 Selenium 进行测试时,我确信这是有道理的,但是当我将它用于自动化时,我想直接在现有配置文件上进行操作。

这对 Selenium 可行吗?实际上,我正在寻找一种方法来维护会话之间的状态(书签、历史记录、Firefox 同步设置等),因此持久会话对我来说最有意义。我在 Python 中做这项工作,但我想其他语言中使用的模式会是相似的。

标签: pythonseleniumfirefox

解决方案


尝试这个

from selenium import webdriver

myprofile = webdriver.FirefoxProfile(r'C:\Users\{YOUR OWN USERNAME}\AppData\Roaming\Mozilla\Firefox\Profiles\moskcpdq.SeleniumTest')
driver = webdriver.Firefox(firefox_profile=myprofile, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
driver.quit()

确保您使用正确的配置文件路径!


推荐阅读