如何在python中保存Firefox webdriver会话

时间:2017-07-25 12:17:30

标签: python selenium webdriver

为了保存chromedriver会话,我使用了以下代码片段:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('user-data-dir= path to where to save session')
driver = webdriver.Chrome(executable_path='path to chromedriver.exe', chrome_options=options)

我尝试用Firefox做同样的事情,但它似乎不起作用:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.add_argument('user-data-dir= path to where to save session')
driver = webdriver.Firefox(executable_path='path to geckodriver.exe', firefox_options=options)

这是正确的方式还是我错过了什么?

1 个答案:

答案 0 :(得分:0)

以下是创建新配置文件并使用现有配置文件启动 Firefox 的手动过程:

  1. 要创建新配置文件,请执行命令:firefox.exe -CreateProfile JoelUser

  2. 要在另一个目录中创建新配置文件,请执行命令:firefox.exe -CreateProfile "JoelUser c:\internet\joelusers-moz-profile"

  3. 要使用新配置文件启动 Firefox,请执行命令:firefox.exe -P "Joel User"

现在,为了以编程方式实现相同的目标,我想出了第 1 步。 1 或 2 可以使用子流程和步骤号执行。 3 可以通过以下 https://stackoverflow.com/a/54065166/6278432

实现

参考文献:

  1. firefox 错误 - 无法使用自定义配置文件创建新会话:https://github.com/mozilla/geckodriver/issues/1058
    https://bugzilla.mozilla.org/show_bug.cgi?id=1421766

  2. Firefox 命令行参数 - https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options#user_profile

  3. 子进程 API 文档 - https://docs.python.org/3/library/subprocess.html