selenium - :driver_opts 已弃用。将 :service 与 Selenium::WebDriver::Service 实例一起使用

标签 selenium selenium-webdriver selenium-chromedriver capybara

2019-06-19 15:44:43 WARN Selenium [DEPRECATION] :driver_opts is deprecated. Use :service with an instance of Selenium::WebDriver::Service instead.
Screenshot error, but continue to execute.
wrong number of arguments (given 0, expected 1)
2019-06-19 15:44:43 WARN Selenium [DEPRECATION] :driver_opts is deprecated. Use :service with an instance of Selenium::WebDriver::Service instead.
2019-06-19 15:44:43 WARN Selenium [DEPRECATION] :driver_opts is deprecated. Use :service with an instance of Selenium::WebDriver::Service instead.
          visit in hook, after scenario +1s, @1s
2019-06-19 15:44:43 WARN Selenium [DEPRECATION] :driver_opts is deprecated. Use :service with an instance of Selenium::WebDriver::Service instead.

我一直在使用下面的代码片段来使用旧的“selenium-webdriver”v3.6.0 启动 session ,并收到上述错误:

于 6 月 23 日更新。

if Capybara.default_driver == :selenium
    Capybara.register_driver :selenium do |app|
        # In the block, we build up an `options` hash to pass to
        #   Capybara::Selenium::Driver.new(app, options)
        # which in turn calls
        #   Selenium::WebDriver.for(options[:browser], options)

        browser = Configuration.fetch('browser.type', :firefox) 
        options = {
            browser: browser, # chrome
        }

        if Configuration.fetch('options.webdriver.use_hub', false)
            {...}
        elsif browser == :firefox
            {...}
        elsif browser == :chrome
            chrome_logpath = "../chromedriver.log"
            options[:service] = ::Selenium::WebDriver::Service.chrome(
                args: {
                    verbose: true,
                    log_path: chrome_logpath,
                }
            )
            chrome_options = Selenium::WebDriver::Chrome::Options.new
            chrome_options.add_argument("user-agent='QA Test'")
            chrome_options.add_option('w3c',false)
            options[:options] = chrome_options
        end
    Capybara::Selenium::Driver.new(app, options)
    end
end

将该 gem 升级到 v3.142.0 后,我收到了该错误。追溯到 https://github.com/SeleniumHQ/selenium/blob/master/rb/CHANGES 中的 Selenium Webdriver 变更日志然后我发现以下描述,这可能会破坏当前的代码 3.141.592(2019-04-18)

Chrome: 
Added support for instantiating service class directly and moved all driver executable configuration there (command-line arguments, port, etc.)
Passing driver_opts, driver_path and port to driver initializer is now deprecated 
so use Selenium::WebDriver::Service.chrome instead, 
which allows to customize executable behavior in similar way.
 Once initialized, this object can be passed as :service keyword 
during driver initialization. 
* Deprecated Chrome.driver_path= in favor of Service::Chrome.driver_path=

谷歌搜索了一段时间,我发现了一些结果和解决方法,例如使用“webdriver”gem,但我不太喜欢它。

那么想知道我是否可以更改上面的代码片段以适应 selenium-webdriver 版本 3.142.0 及更高版本?我现在使用的是 Capybara v3.18.0。

谢谢大家,

最佳答案

这不是错误,而是弃用警告。它告诉您在 selenium-webdriver v4.0 发布之前需要更改代码。如果您觉得今天必须更新代码,那就是这样的

elsif browser == :chrome

  options[:service] = ::Selenium::WebDriver::Service.chrome(
    args: {
      verbose: true,
      log_path: "../chromedriver.log",
    }
  )

  chrome_options = Selenium::WebDriver::Chrome::Options.new

  # add user agent to that class options
  chrome_options.add_argument("user-agent='QA Test'")
  options[:options] = chrome_options
end

您展示的其他内容

Screenshot error, but continue to execute.
wrong number of arguments (given 0, expected 1)

是不同的东西,不是来自您所显示的任何代码。

关于selenium - :driver_opts 已弃用。将 :service 与 Selenium::WebDriver::Service 实例一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56712867/

相关文章:

selenium - Selenium :如何等待选择中的选项被填充?

python - Python-Behave 中屏幕截图的自定义错误处理程序

java - Selenium-根据文本匹配获取列表索引号

c# - 如何使用 Selenium WebDriver 将缩放重置为 Chrome 上的默认值?

c# - 即使元素存在,ExpectedConditions.ElementIsVisible 也会返回 TimeoutException

python - 使用类名、css-选择器和 xpath 从网站中查找元素

python - 你如何在 pytest session 结束时清理资源?

java - 访问 selenium webdriver javascriptexecutor 中的 java 变量

java - 使用 Selenium 从只读文本框中获取动态文本

c# - 无法将 DesiredCapabilities 传递给 ChromeDriver :s constructor?