【问题标题】:Setting the user agent in Firefox using geckodriver v0.11使用 geckodriver v0.11 在 Firefox 中设置用户代理
【发布时间】:2017-04-19 13:46:02
【问题描述】:

我正在努力更新我们的 selenium 测试以与 Firefox v50.x 一起使用,因此我需要使它们与 geckodriver v0.11 兼容

现在看来,现在使用 Firefox::Profile 初始化 webdriver 会使 webdriver 崩溃,因为它无法识别配置文件中设置的任何字段:

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={}>}

我仍然可以通过不使用配置文件初始化 webdriver 来运行测试,但是我们的一些测试将会失败,因为它们需要我们有一个特定的用户代理。

阅读文档后,您现在似乎应该使用 moz:firefoxOptions 来实现此功能,但我找不到任何代码示例来说明如何实际使用 moz:firefoxoptions 来完成任何事情

谁能给我一个代码示例,展示如何使用 moz:firefoxOptions 或任何其他方式使用 selenium-webdriver 3.0.3 和 geckodriver v0.11 设置 firefox 用户代理?

【问题讨论】:

    标签: ruby selenium firefox geckodriver


    【解决方案1】:

    目前您无法通过 geckodriver 的配置文件选项传递配置文件。这记录在Issue #2933 中,我目前有一个 PR (PR #3174) 用于添加此功能。

    目前,您应该能够执行以下操作:

    通过任一方式创建个人资料

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

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

    然后

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

    [:firefox_options] 在代码中被翻译成moz:firefoxOptions

    附带说明,您也可以使用旧版 FF 驱动程序而不是 geckodriver 运行,因为您运行的是 FF50

    【讨论】:

    • 谢谢!必须在 Firefox 中为我们希望使用的不同用户代理创建多个配置文件有点麻烦,但这似乎可行
    猜你喜欢
    • 2018-02-07
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 2013-11-16
    相关资源
    最近更新 更多