webdriver.FirefoxProfile():是否可以使用配置文件而不复制它?

Mat*_* M. 4 selenium automation webdriver python-3.x selenium-webdriver

正如文档所述,您可以使用可选参数调用 webdriver.FirefoxProfile()profile_directory以指向您希望浏览器使用的特定配置文件的目录。我注意到运行这个命令需要很长时间,所以当我查看代码时,它看起来像是在复制指定的配置文件 问题是,配置文件复制需要很长时间(大约 >30 分钟,没有耐心等待它完成。)

我正在使用用户脚本和 selenium 的混合体来为我做一些自动化,所以每次我想测试我的代码时都设置一个新的配置文件会很麻烦。

是改变这种行为以编辑firefox_profile.py自身的唯一方法(如果是这样,最好的方法是什么?)?

Deb*_*anB 5

根据GeckoDriverFirefox的当前实现,使用FirefoxProfile()如下工作:

  • 如果通过新的Firefox 配置文件启动浏览会话,如下所示:

    from selenium import webdriver
    
    myprofile = webdriver.FirefoxProfile()
    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()
    
    Run Code Online (Sandbox Code Playgroud)
  • 运行时会创建一个新的rust_mozprofile,如下所示:

    1521446301607   mozrunner::runner   INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile" "C:\\Users\\ATECHM~1\\AppData\\Local\\Temp\\rust_mozprofile.xFayqKkZrOB8"
    
    Run Code Online (Sandbox Code Playgroud)
  • Of-coarse 成功关闭(即成功调用driver.quit())临时rust_mozprofile.xFayqKkZrOB8被完全删除/销毁。

  • 再次在通过现有的Firefox Profile()启动浏览会话的情况下,如下所示:

    from selenium import webdriver
    
    myprofile = webdriver.FirefoxProfile(r'C:\Users\AtechM_03\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()
    
    Run Code Online (Sandbox Code Playgroud)
  • 类似地,运行时会创建一个新的rust_mozprofile,如下所示:

    1521447102321   mozrunner::runner   INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile" "C:\\Users\\ATECHM~1\\AppData\\Local\\Temp\\rust_mozprofile.2oSwrQwQoby9"
    
    Run Code Online (Sandbox Code Playgroud)
  • 同样,在这种情况下,在成功关闭(即成功调用driver.quit())时,临时rust_mozprofile.2oSwrQwQoby9 也会被完全删除/销毁。

  • 所以你观察的时间跨度是 aFirefoxProfile()挖出一个新的rust_mozprofile所需的时间。

也许根据您的问题,复制配置文件的时间跨度(例如> 30 分钟)是纯粹的开销。因此,这将是不可能的使用Firefox的配置文件而不进行的副本rust_mozprofile


解决方案

  • Selenium Client升级到当前级别Version 3.11.0
  • 升级GeckoDriver当前GeckoDriver v0.20.0水平。
  • Firefox版本升级到Firefox Quantum v59.0.1级别。
  • 清理你的项目工作,通过你的IDE重建仅需要依赖你的项目。
  • 在执行测试套件之前和之后,使用CCleaner工具清除所有操作系统杂务。
  • 如果您的基础Firefox基础版本太旧,请通过Revo Uninstaller卸载它并安装最新的 GA 和Firefox Quantum发布版本。
  • 执行您的@Test.

  • 所有“解决方案”都没有解决用户的问题:如何让 Firefox 使用指定的配置文件*而不在每次启动时复制*它。 (5认同)