【问题标题】:How to save Firefox webdriver session in python如何在 python 中保存 Firefox webdriver 会话
【发布时间】:2017-12-31 08:46:49
【问题描述】:

为了保存 chromedriver 会话,我使用了这个 sn-p 代码:

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)

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

【问题讨论】:

标签: python selenium webdriver


【解决方案1】:

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

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

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

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

现在,为了以编程方式实现相同的目标,我想出了第一个步骤。可以使用子流程和步骤号执行 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 2010-10-31
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多