使用capybara捕获浏览器控制台日志

Viv*_*ana 7 javascript ruby selenium capybara console.log

我需要使用Ruby和Capybara捕获浏览器的控制台日志(类别:info).直到现在我已经尝试过使用driver.manage.logs.get(:browser)或(:client),但是使用它,结果不是我想要的.它给出了selenium和浏览器之间的交互结果,我可以看到我的javascript语句被发送执行,但结果输出无法捕获.

需要帮忙!!!

Tho*_*ole 19

使用selenium时是否可以使用日志取决于您使用Selenium的浏览器.如果你使用的是Firefox,那你就不幸了,因为它不支持日志检索API,但是因为你使用的是Chrome,所以它们是可访问的.您遇到的问题是,默认情况下,仅捕获WARN或ERROR级别日志.您可以通过loggingPrefs功能在驱动程序注册中更改此设置

Capybara.register_driver :logging_selenium_chrome do |app|
  caps = Selenium::WebDriver::Remote::Capabilities.chrome(loggingPrefs:{browser: 'ALL'})
  browser_options = ::Selenium::WebDriver::Chrome::Options.new()
  # browser_options.args << '--some_option' # add whatever browser args and other options you need (--headless, etc)
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options, desired_capabilities: caps)
end
Run Code Online (Sandbox Code Playgroud)

然后指定:logging_selenium_chrome用作您的驱动程序

 Capybara.javascript_driver = :logging_selenium_chrome # or however else you're specifying which driver to use
Run Code Online (Sandbox Code Playgroud)

这应该允许您在测试中获取日志

page.driver.browser.manage.logs.get(:browser)
Run Code Online (Sandbox Code Playgroud)

  • 不适用于“Google Chrome 85.0.4183.83”和“ChromeDriver 85.0.4183.83”“NoMethodError:私有方法“log”调用#&lt;Selenium::WebDriver::Remote::Bridge:0x00007fe77c1d47``f8&gt;” (2认同)

0Ds*_*Ds0 7

Thomas Walpole 的回答是正确的,但现在看来,如果您使用 chrome 作为驱动程序,则应该使用

Selenium::WebDriver::Remote::Capabilities.chrome( "goog:loggingPrefs": { browser: 'ALL' } )
Run Code Online (Sandbox Code Playgroud)

请注意goog:loggingPrefsloggingPrefs我能够console.log在日志中打印,而不是仅使用此解决方案。

经过几次令人沮丧的尝试,我花了一段时间从这里https://intellipaat.com/community/5478/getting-console-log-output-from-chrome-with-selenium-python-api-bindings得到它。


归档时间:

查看次数:

5086 次

最近记录:

6 年,4 月 前