使用 Watir-Webdriver/Selenium 在 Firefox 中指定自定义用户数据目录

dar*_*mkd 6 ruby firefox selenium watir-webdriver selenium-webdriver

我可以使用 Chrome 驱动程序执行以下操作:

b = Watir::Browser.new :chrome, :switches => ['--user-data-dir=C:/some_folder/'] # same philosophy for selenium, just a bit of a different syntax.
Run Code Online (Sandbox Code Playgroud)

这将创建一个新的用户数据目录,其中将存储所有 cookie、书签、缓存等。基本上,创建一个新的配置文件。如果这样的文件夹不存在,它将创建它。如果它确实存在,它将从中加载cookies/所有相关文件。

有没有办法使用 Firefox 驱动程序来做同样的事情?我一直在研究创建 Firefox 配置文件的方法,我发现的只是这篇文章:创建新的 Firefox 配置文件,它不能解决我的问题,因为我希望自动完成它,就像上面的 Chrome 驱动程序一样。另外,您似乎可以使用以下命令创建新的配置文件:

profile = Selenium::WebDriver::Firefox::Profile.new
Run Code Online (Sandbox Code Playgroud)

但我还没有找到用我指定的名称保存该配置文件的方法。

ako*_*nov 5

这适用于 Firefox 60+ 和新的marionette/geckodriver

经过一番摆弄之后,这就是我如何使用自定义配置文件来实现它:

$ xvfb-run firefox -CreateProfile test
options = Selenium::WebDriver::Firefox::Options.new # no profile specified here
options.add_argument "--profile"
options.add_argument "/home/jenkins/.mozilla/firefox/f0jecsmr.test"
@browser = Watir::Browser.new :firefox, options: options, driver_opts: {marionette_port: 2828}, **capabilities
Run Code Online (Sandbox Code Playgroud)

它与文档建议的使用 python 的方式很接近。不过,我需要阅读源代码才能弄清楚这些driver_opts(或者我没有找到相关文档)。

HTH某人。


Jus*_* Ko 2

基于 Selenium Issue 1954Issue 7374,Firefox 驱动程序当前不具有此功能。鉴于一些项目成员反对这一想法,很难判断是否会实施。

目前,我认为您必须对 Selenium-WebDiver 版本进行猴子修补才能添加此功能。我得到了和@shri046的答案相同的结论,就是修改layout_on_disk方法。

需要 Selenium-WebDriver 后,为 Selenium::WebDriver::FirefoxProfile 添加以下猴子补丁。这个补丁的逻辑是:

  • 如果未指定配置文件目录,请使用正常/现有行为,这意味着复制现有配置文件并在退出时将其删除。
  • 如果指定了配置文件目录:
    • 如果该目录存在,则使用该目录作为配置文件。假设该目录是有效的配置文件(即我没有添加检查以确保其有效)。
    • 如果该目录不存在,则会创建现有配置文件的副本(即正常行为)。然后该配置文件将被移动到指定的目录。该配置文件在退出时不会被删除,因此可以在以后再次使用。

修补:

require 'watir-webdriver'
require 'selenium-webdriver'

module Selenium
  module WebDriver
    module Firefox
      class Profile
        class << self
          attr_accessor :webdriver_profile_directory
        end

        def layout_on_disk
          # When a directory is specified, ensure it is not deleted at exit
          if Profile.webdriver_profile_directory
            FileReaper.reap = false
          end

          # Use the specified directory if it already exists (ie assuming an existing profile)
          if Profile.webdriver_profile_directory && Dir.exists?(Profile.webdriver_profile_directory)
            return Profile.webdriver_profile_directory
          end

          # Create the profile directory as usual when it does not exist
          profile_dir = @model ? create_tmp_copy(@model) : Dir.mktmpdir("webdriver-profile")
          FileReaper << profile_dir

          install_extensions(profile_dir)
          delete_lock_files(profile_dir)
          delete_extensions_cache(profile_dir)
          update_user_prefs_in(profile_dir)

          # If a directory is specified, move the created profile to that directory
          if Profile.webdriver_profile_directory
            FileUtils.cp_r(profile_dir, Profile.webdriver_profile_directory)
            profile_dir = Profile.webdriver_profile_directory
          end

          profile_dir
        end
      end # Profile
    end # Firefox
  end # WebDriver
end # Selenium
Run Code Online (Sandbox Code Playgroud)

要指定要使用的配置文件位置,请执行以下操作:

Selenium::WebDriver::Firefox::Profile.webdriver_profile_directory = 'C:/temp/test-profile'
browser = Watir::Browser.new :firefox
Run Code Online (Sandbox Code Playgroud)

该补丁可能有局限性和未经测试的区域。例如,它可能无法处理创建 Firefox 的多个实例。然而,对于单个实例,它至少似乎可以重新加载创建的书签(这是我的测试的限制)。