Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new webdriver session manually

I am trying to play a little bit with WebDriver Protocol using Python and requests module.

I started chromedriver binary:

$ chromedriver_2.31 
Starting ChromeDriver 2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8) on port 9515
Only local connections are allowed.

So far so good.

But the problem is that I get session not created exception when trying to create a session:

import requests

r = requests.post("http://127.0.0.1:9515/session", {})

print("Status: " + str(r.status_code))
print("Body: " + str(r.content))

Execution output:

Status: 200
Body: b'{"sessionId":"286421fcd381ee0471418ebce7f3e125","status":33,"value":{"message":"session not created exception: Missing or invalid capabilities\\n  (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Linux 4.4.0-91-generic x86_64)"}}'

I searched through the WebDriver Protocol docs, but I couldn't find information about which capabilities are mandatory or something like that.

So, I tried with some random capabilities:

import requests

data = {
        "browserName": "chrome",
        "version": "",
        "platform": "LINUX",
        "javascriptEnabled": "true",
        "acceptInsecureCerts": "false",
        "cssSelectorsEnabled": "true"
        }

r = requests.post("http://127.0.0.1:9515/session", data)

print("Status: " + str(r.status_code))
print("Body: " + str(r.content))

But again fail:

Status: 400
Body: b'missing command parameters'

Do you have any ideas what is the problem and how to fix it?

UPDATE

Also tried:

import requests


data = """
        {
            desiredCapabilities: {  
            "browserName": "chrome",
            "version": "",
            "platform": "ANY"
            }
        }
    """

headers = {'Content-type': 'application/json'}

r = requests.post("http://127.0.0.1:9515/session", json=data, headers=headers)

print("Status: " + str(r.status_code))
print("Body: " + str(r.content))

Again error:

Status: 400
Body: b'missing command parameters'
like image 466
CuriousGuy Avatar asked Mar 14 '26 05:03

CuriousGuy


2 Answers

Okay, so I looked at how Selenium does it..:

selenium/webdriver/remote/remote_connection.py, class RemoteConnection, method execute. I raised an exception for params, like this:

raise Exception(params)

Here's what came out:

{'capabilities': {'alwaysMatch': {'browserName': 'chrome',
   'chromeOptions': {'args': [], 'extensions': []},
   'platform': 'ANY',
   'version': ''},
  'firstMatch': []},
 'desiredCapabilities': {'browserName': 'chrome',
  'chromeOptions': {'args': [], 'extensions': []},
  'platform': 'ANY',
  'version': ''}}

So, doing the same thing, with data our new found dictionary:

r = requests.post('http://127.0.0.1:9515/session', json=data)
# A new chromium window is created
r.content
`b'{"sessionId":"94cf6af9577d323eb51f6340b1fd2d07","status":0,"value":{"acceptSslCerts":true,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"browserName":"chrome","chrome":{"chromedriverVersion":"2.30.477729 (e5aa99d9d101379b1542958a71df3f50913f1ea2)","userDataDir":"/tmp/.org.chromium.Chromium.uqwViV"},"cssSelectorsEnabled":true,"databaseEnabled":false,"handlesAlerts":true,"hasTouchScreen":false,"javascriptEnabled":true,"locationContextEnabled":true,"mobileEmulationEnabled":false,"nativeEvents":true,"networkConnectionEnabled":false,"pageLoadStrategy":"normal","platform":"Linux","rotatable":false,"takesHeapSnapshot":true,"takesScreenshot":true,"unexpectedAlertBehaviour":"","version":"60.0.3112.78","webStorageEnabled":true}}'`
like image 163
Jugurtha Hadjar Avatar answered Mar 15 '26 19:03

Jugurtha Hadjar


You need to post json data containing capabilities and desired capabilities. alwaysMatch can contain any or more of the following:

  • acceptInsecureCerts
  • browserName
  • browserVersion
  • platformName
  • pageLoadStrategy
  • proxy
  • setWindowRect
  • timeouts
  • unhandledPromptBehavior


    params = {
        'capabilities': {
            'firstMatch': [{}], 
            'alwaysMatch': {
                ...
            }
        },
        'desiredCapabilities': {
            'browserName': 'chrome',
            'version': '60',
            'platform': 'MAC'
        }
    }

r = requests.post('http://127.0.0.1:9515/session', json=params)

Reference:

  • Selenium Command
  • Desired Capabilities
like image 30
Oluwafemi Sule Avatar answered Mar 15 '26 19:03

Oluwafemi Sule



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!