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

时间:2018-03-19 05:01:36

标签: python-3.x selenium selenium-webdriver automation webdriver

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

我正在使用用户脚本和selenium的混合为我做一些自动化,所以每次我想测试我的代码时设置一个新的配置文件将是繁重的。

是改变此行为以编辑firefox_profile.py本身的唯一方法(如果是,那么最好的方法是什么?)?

1 个答案:

答案 0 :(得分:2)

根据 GeckoDriver 的当前实现 Firefox 使用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()
    
  • 在运行中创建新的 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"
    
  • 成功关闭(即成功调用driver.quit())粗略的 rust_mozprofile.xFayqKkZrOB8 会被完全删除/销毁。

  • 再次通过现有 Firefox个人资料()启动浏览会话,如下所示:

    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()
    
  • 同样,在运行中创建了一个新的 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"
    
  • 同样在这种情况下,成功关闭(即成功调用driver.quit()),临时 rust_mozprofile.2oSwrQwQoby9 将被完全删除/销毁。

    < / LI>
  • 因此,您观察的 timespan FirefoxProfile() 挖出新 rust_mozprofile 所需的时间。

也许根据您的问题,要复制的配置文件的时间跨度(类似于> 30分钟)是纯粹的开销。但,如果不复制 rust_mozprofile ,就无法使用 Firefox个人资料

解决方案

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