Session not created Selenium/webdriver when using

2019-07-26 16:05发布

问题:

Since upgrading to Safari 12, my automated scripts are now getting this error:

SessionNotCreatedError: Request body does not contain required parameter 'capabilities'.

(The error does not occur for other browsers).

I'm using the javascript webdriver bindings and, when I build webdriver, I use the withCapability key value pairs:

var capabs = {
       'browserName' : 'Safari',
       'version' : '12.0'
    }
    browserUnderTest = new webdriver.Builder().
    withCapabilities(capabs)    
    .forBrowser('safari')
    .build();

I think the problem is with the safari.js file itself, but I don't know enough about how it operates to pinpoint anything. Here is the full text of the error:

SessionNotCreatedError: Request body does not contain required parameter 'capabilities'.
    at Object.throwDecodedError (/Users/qualit/Documents/autotests/node_modules/selenium-webdriver/lib/error.js:514:15)
    at parseHttpResponse (/Users/qualit/Documents/autotests/node_modules/selenium-webdriver/lib/http.js:519:13)
    at doSend.then.response (/Users/qualit/Documents/autotests/node_modules/selenium-webdriver/lib/http.js:441:30)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
From: Task: WebDriver.createSession()
    at Function.createSession (/Users/qualit/Documents/autotests/node_modules/selenium-webdriver/lib/webdriver.js:769:24)
    at Function.createSession (/Users/qualit/Documents/autotests/node_modules/selenium-webdriver/safari.js:253:41)
    at createDriver (/Users/qualit/Documents/autotests/node_modules/selenium-webdriver/index.js:170:33)
    at Builder.build (/Users/qualit/Documents/autotests/node_modules/selenium-webdriver/index.js:660:16)
    at Object.<anonymous> (/Users/qualit/Documents/autotests/K8_autotest.js:354:6)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)

Does anyone have any ideas about the cause of this or a fix?

回答1:

This issue happens because Safari 12 uses a new W3C webdriver protocol (source) which appears to be incompatible with the latest stable selenium-webdriver package (v3.6)

safaridriver can be passed a --legacy flag to use the old protocol. Directly on the command line this would be done like: /usr/bin/safaridriver --legacy

This flag can be set on the driver in your node program as follows:

const webdriver = require('selenium-webdriver');
const safari = require('selenium-webdriver/safari');

new webdriver.Builder()
    .usingServer(await new safari.ServiceBuilder().addArguments('--legacy').build().start())
    .forBrowser('safari')
    .build();

Here's documentation on the ServiceBuilder API - https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/safari_exports_ServiceBuilder.html

A couple GitHub tickets cover this as well:

  • https://github.com/SeleniumHQ/selenium/issues/6431
  • https://github.com/SeleniumHQ/selenium/issues/6026


回答2:

This will also work if you get an error for 'await' when trying @mjdease solution above.

new webdriver.Builder()
    .usingServer(new safari.ServiceBuilder().addArguments('--legacy').build().start())
    .forBrowser('safari')
    .build();