Search code examples
rubyseleniumfirefoxgeckodriver

Setting the user agent in Firefox using geckodriver v0.11


I am working on updating our selenium tests to function with Firefox v50.x and thus I need to make them compatible with geckodriver v0.11

It seems now that now initializing the webdriver using a Firefox::Profile makes the webdriver crash as it does not recognize any of the fields set in the profile:

ArgumentError
unknown option: {:profile=>#<Selenium::WebDriver::Firefox::Profile:0x00000005088bf8 @model="selenium_profile", @native_events=false, @secure_ssl=false, @untrusted_issuer=true, @load_no_focus_lib=false, @additional_prefs={}, @extensions={}>}

I can still make the tests run by just not initializing the webdriver with a profile, but some of our tests will then fail as they require that we have a specific user agent.

After reading the documentation it seems that you should now use moz:firefoxOptions for this functionality, but I can't find any code examples showing how do actually use moz:firefoxoptions for anything

Could anyone give me a code example showing how to use moz:firefoxOptions or any other way of setting the firefox user agent with selenium-webdriver 3.0.3 and geckodriver v0.11?


Solution

  • Currently you cannot currently pass a profile via the profile option for geckodriver. This is documented in Issue #2933, and I currently have a PR (PR #3174) in for adding this functionality.

    For now, you should be able to do the following:

    Create the profile by either

    profile = Selenium::WebDriver::Firefox::Profile.new('/path/to/profile')
    

    or

    profile = Selenium::WebDriver::Firefox::Profile.from_name('name_of_profile')
    

    then

    caps = Selenium::WebDriver::Remote::W3CCapabilities.firefox
    caps[:firefox_options] = { profile: profile.as_json['zip'] }
    driver = Selenium::WebDriver.for(:firefox, desired_capabilities: caps)
    

    [:firefox_options] gets translated to moz:firefoxOptions in the code

    On a side note, you could also run with the legacy FF driver instead of geckodriver since you are running against FF50