Selenium: Trying to get Firefox console logs results in “WebDriverError: HTTP method not allowed”

主宰稳场 提交于 2021-02-07 09:54:03

问题


I'm trying to capture console errors in my selenium tests (node environment with selenium-webdriver 4.0.0-alpha.5 and the latest geckodriver and chromedriver). I've set up one driver for Firefox and another one for Chrome like this:

const chrome = require('selenium-webdriver/chrome');
const firefox = require('selenium-webdriver/firefox');
const webdriver = require('selenium-webdriver');
const { Builder, By, Capabilities, until } = webdriver;

let loggingPref = new webdriver.logging.Preferences();
loggingPref.setLevel( 'browser', webdriver.logging.Level.SEVERE );

let driver_fx = await new Builder()
  .withCapabilities(
    Capabilities.firefox()
    .set("acceptInsecureCerts", true)
  )
  .setLoggingPrefs( loggingPref )
  .build();

let driver_chr = await new Builder()
  .forBrowser('chrome')
  .setLoggingPrefs( loggingPref )
  .build();

This is the function that should get the error logs:

const getConsoleErrors = (driver) => {
  return driver.manage().logs().get('browser').then((logs) => {
    return logs.map(( log ) => log.message );
  });
}

With the chrome driver, this works as intended:

await driver.get(devUrl);

let errors = await getConsoleErrors(driver_chr);
console.log(errors);
// output:
// [ 'https://mylocaldevserver/with/path 465:61 Uncaught TypeError: Cannot read property \'textContent\' of null' ]

However, when passing driver_fx to the function instead, this results in the following exception:

WebDriverError: HTTP method not allowed
at parseHttpResponse (***\node_modules\selenium-webdriver\lib\http.js:580:11)
at Executor.execute (***\node_modules\selenium-webdriver\lib\http.js:489:26)
at process._tickCallback (internal/process/next_tick.js:68:7)

Is this a bug in selenium or geckodriver, or is it the way I construct the Firefox driver differently (which I need to do for it to ignore the certificate of my local dev server)?


回答1:


For future googlers, unfortunately log('browser') is not in W3C specification, hence the geckodriver team seems to be stalling it. There is no official "fix" for it as of today (Feb 17 2020).

You'll most likely have to use a work around like: https://github.com/mozilla/geckodriver/issues/284#issuecomment-477677764

Find more information here: https://github.com/mozilla/geckodriver/issues/284



来源:https://stackoverflow.com/questions/59192232/selenium-trying-to-get-firefox-console-logs-results-in-webdrivererror-http-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!