Selenium 4.x trying to POST CDP: "UnsupportedCommandException" - selenium

I`m trying to execute some commands via CDP, however no matter what combination of Selenium/Driver/Chrome I use it's always the same result.
Last tested with:
Selenium 4.1.1
Chrome + Driver 96.0.4664.110
The project is made in C so I am posting manually to Selenium via CURL. Every other command besides CDP works fine.
I have checked Selenium, Chrome Driver; they both have the CDP support built in.
The URL's I tried to post to are:
- /session/id/goog/cdp/execute
- /session/id/{}/cdp/execute
The posted data format is: "cmd" + "params" (json object).
Both end in the same result: org.openqa.selenium.UnsupportedCommandException.
I also tried to run Selenium in different modes, standalone, hub/node, same result.
Can someone please advise what I am doing wrong? Or maybe I have misunderstood the usage?

Using chromedriver executable
This worked for me (Windows + Postman), but should also work with CURL Linux/Mac.
1 Download chromedriver: https://chromedriver.chromium.org/downloads for your chrome version.
2 Start chromedriver
start chromedriver.exe
output:
Starting ChromeDriver 97.0.4692.71 on port 9515...
3 Send requests to localhost:9515/
3.1 Create Session:
POST localhost:9515/session
request json body:
{"capabilities":{"goog:chromeOptions": {}}}
status 200
response:
"value": {
"capabilities": {
...
},
"sessionId": "b8ac49ce2203739fa0d32dfe8d1a23b5"
3.2 Navigate some url (optional, just check request by sessionId works):
POST localhost:9515/session/b8ac49ce2203739fa0d32dfe8d1a23b5/url
request json body:
{"url": "https://example.com"}
status 200
3.3 Execute CDP command (take screenshot):
POST localhost:9515/session/b8ac49ce2203739fa0d32dfe8d1a23b5/goog/cdp/execute
request json body:
{"cmd":"Page.captureScreenshot", "params":{}}
status 200
response:
{
"value": {
"data": "iVBORw0KGgoAAAANSUhEUgA...."
}
}
Allow remote connections
By default chromedriver allows only local connections.
To allow some remote IPs:
start chromedriver.exe --allowed-ips="some-remote-ip"
Reference: https://sites.google.com/a/chromium.org/chromedriver/security-considerations
Run CDP commands with Selenium Grid
Eventually, it started to work for me with
ChromeDriver 97.0.4692.71
selenium-server-4.1.1
Chrome 97.0.4692.71 (Official Build) (64-bit)
Note: Content-Type header should have charset=utf-8
Content-Type:application/json;charset=utf-8 for Selenium Grid HTTP requests.
Prerequisites
1 Download and run selenium server according to
https://www.selenium.dev/documentation/grid/getting_started/
java -jar selenium-server-<version>.jar standalone --driver-configuration display-name='Chrome' stereotype='{"browserName":"chrome"}'
2 Create Session:
POST localhost:4444/wd/hub/session
request json body:
{
"desiredCapabilities": {
"browserName": "chrome",
"goog:chromeOptions": {
"args": [
],
"extensions": [
]
}
},
"capabilities": {
"firstMatch": [
{
"browserName": "chrome",
"goog:chromeOptions": {
"args": [
],
"extensions": [
]
}
}
]
}
}
status 200
response:
{
"status": 0,
"sessionId": "69ac1c82306f72c7aaf53cfbb28a30e7",
...
}
}
3 Execute CDP command (take screenshot):
POST localhost:4444/wd/hub/session/69ac1c82306f72c7aaf53cfbb28a30e7/goog/cdp/execute
request json body:
{"cmd":"Page.captureScreenshot", "params":{}}
status 200
response:
{
"value": {
"data": "iVBORw0KGgoAAAANSUhEUgA...."
}
}

Related

Use custom firefox browser version capability for geckodriver

I build selenoid docker images for firefox myself and have them configured in the browsers.json as follows:
"firefox": {
"default": "66.0",
"versions": {
"beta": {
"image": "my/path/to/firefox:beta",
"port": "4444"
},
"66.0": {
"image": "selenoid/vnc:firefox_66.0",
"port": "4444"
}
}
}
Sending the version=beta capability causes the webdriver to throw
selenium.WebDriverException: Requested environment is not available
In the logs I found
2019/04/03 08:17:29 [3] [PROXY_TO] [90ab834d22aa3bbe2731eeb550497eec7ef9fb11c1e7f4609d617cf6a25124e7] [http://172.17.0.4:4444]
2019/04/03 08:17:29 [3] [SESSION_ATTEMPTED] [http://172.17.0.4:4444] [1]
2019/04/03 08:17:29 [3] [SESSION_ATTEMPTED] [http://172.17.0.4:4444/wd/hub] [2]
2019/04/03 08:17:29 [3] [SESSION_FAILED] [http://172.17.0.4:4444/wd/hub] [400 Bad Request]
Since the exact same thing works for chrome and it also works if I set "default": "beta" in the browsers.json and do not set the version capability, I assume this happens because geckodrivers matches the version from the capabilites against the actual browser version. (as presumed here).
I have several ideas how to work around this, but do not know how to technically implement these:
Prevent Selenoid from passing the version capability to the selenium driver (geckodriver in this case)
Prevent geckodriver from checking version capability and browser version
Add another flag to selenoid to get the docker image from browsers.json such as browser_version instead of version
Add another fake browser to browsers.json and set the beta to default, then just set the browserName capability. The problem here is, selenoid checks for the browser names and if it does not match chrome, firefox or opera the selenoid container cannot be started. E.g.:
browserName=firefox-beta
"firefox-beta": {
"default": "beta",
"versions": {
"beta": {
"image": "my/path/to/firefox:beta",
"port": "4444"
}
}
}
Any help or further information will be greatly appreciated
For w3c compliant browsers (as of today, only non-Chrome), they require the browser version to be in the browserVersion capability instead of version. I see you tried browser_version, but did you try browserVersion?
https://www.w3.org/TR/webdriver1/#capabilities

My nightwatch.js tests not runs in Chrome headless of CentOS

I run nightwatch.js tests using Nightwatch version 1.0.18 and It's working in windows environment but when I run it in centOS after installment of Xvfb I found below error.
Error while running .navigateTo() protocol action: invalid session id
Error while running .locateMultipleElements() protocol action: invalid session id
Error while running .locateMultipleElements() protocol action: invalid session id
Here is my nightwatch.json file code:
{
"src_folders": [
"./tests"
],
"output_folder": "./reports",
"custom_commands_path": "./custom_commands",
"custom_assertions_path": "",
"test_workers": false,
"webdriver": {
"start_process": true
},
"test_settings": {
"default": {
"webdriver": {
"port": 9515,
"server_path": "./node_modules/chromedriver/lib/chromedriver/chromedriver",
"cli_args": [
"--log",
"debug"
]
},
"skip_testcases_on_fail": true,
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true,
"chromeOptions": {
"args": [
"headless",
"no-sandbox",
"disable-gpu"
]
}
}
}
}
}
am I missing something to run my tests in the centOS environment because it is running in the windows environment?
I had the same issue with Nightwatchjs and the npm chomedriver setup.
Background:
Everything was working until I just recently updated Chromium on my system. In addition to the errors in the original post, verbose logging also showed:
{
message: 'unknown error: Chrome failed to start: exited abnormally',
error: [
"(unknown error: DevToolsActivePort file doesn't exist)",
'(The process started from chrome location /usr/bin/chromium is no longer running, so ChromeDriver is assuming that Chrome has crashed.)',
'(Driver info: chromedriver=2.46.628388 (4a34a70827ac54148e092aafb70504c4ea7ae926),platform=Linux 4.9.0-8-amd64 x86_64)'
],
}
After downloading the standalone chromedriver (2.46.628388) to match my Chromium version (72.0.3626.69) it was still showing the same errors.
Solution:
I ended up downloading an older version of Chromium (71.0.3578.127) and setting chromeOptions.binary to the new path of the chromium 71 binary. I also had to include 'no-sandbox' with chromeOptions.args.
Here is the snippet from the site mentioned above:
Downloading old builds of Chrome / Chromium
Let's say you want a build of Chrome 44 for debugging purposes. Google does not offer old builds as they do not have up-to-date security fixes.
However, you can get a build of Chromium 44.x which should mostly match the stable release. Here's how you find it:
Look in https://googlechromereleases.blogspot.com/search/label/Stable%20updates for the last time "44." was mentioned.
Loop up that version history ("44.0.2403.157") in the Position Lookup
In this case it returns a base position of "330231". This is the commit of where the 44 release was branched, back in May 2015.*
Open the continuous builds archive
Click through on your platform (Linux/Mac/Win)
Paste "330231" into the filter field at the top and wait for all the results to XHR in.
Eventually I get a perfect hit: https://commondatastorage.googleapis.com/chromium-browser-snapshots/index.html?prefix=Mac/330231/
Sometimes you may have to decrement the commit number until you find one.
Download and run!
Upgrading to the latest version of chromedriver solved the issue for me. You can find the latest version here; https://www.npmjs.com/package/chromedriver
In my situation, when that error occurs:
Error while running .navigateTo() protocol action: invalid session id
I added the following code into .travis.yml:
addons:
chrome: stable

selenium find element by id with firefox failed

I'm using Windows MobaXTerm for execute command. i execute Selenium by this command :
cd /drives/c/selenium; java -Dhttp.proxyHost=http://proxy.wideip -Dhttp.proxyPort=3128 -Dwebdriver.gecko.driver="C:\selenium\geckodriver.exe" -jar selenium-server-standalone-3.8.1.jar -port 4444
My geckodriver is into version 0.20 (the last actualy) My selenium version is greater then 3 and in fact include marionette.
For try to identify my problem i'm using postman.
Step 1 : Create session
Step 2 : set Url
Step 3 : get element by id
In step get element by id i have this result (in chrome)
{
"sessionId": "448e264843b4bbf506f03491b2bf940d",
"status": 0,
"value": {
"ELEMENT": "0.28994850647447534-1"
}
}
and this result in firefox :
{
"state": "success",
"sessionId": null,
"hCode": 23764448,
"value": {
"element-6066-11e4-a52e-4f735466cecf": "17752648-8258-4338-8a0e-40be95c874c4"
},
"class": "org.openqa.selenium.remote.Response",
"status": 0
}
For query i'm using same :
http://localhost:4444/wd/hub/session/{{selenium_session_id}}/element
and for query body i'm sending same :
{"using":"id","value":"id_button_submit"}
Of course i'm using same url / and same try.
But in FF i'm don't have result.value.ELEMENT, and if i don't have it, i can't have driverElementId, and click into, all my try's scenario is failed for FF
I'm using last Developer FF version.
May you give me idea for identify my problem and solve it please ?

Error when running Selenium Server via Nightwatch

I'm attempting to run a simple test script. But I receive the following error:
I have my nightwatch config file setup as so:
nightwatch.conf.js
module.exports = {
"src_folders": [
"tests"// Where you are storing your Nightwatch e2e/UAT tests
],
"output_folder": "./reports", // reports (test outcome) output by nightwatch
"selenium": {
"start_process": true, // tells nightwatch to start/stop the selenium process
"server_path": "./node_modules/selenium-standalone/.selenium/selenium-server/2.53.1-server.jar",
"host": "127.0.0.1",
"port": 4444, // standard selenium port
"cli_args": { "webdriver.chrome.driver" : "./node_modules/selenium-standalone/.selenium/chromedriver/2.25-x64-chromedriver"
}
},
"test_settings": {
"default": {
"screenshots": {
"enabled": true, // if you want to keep screenshots
"path": './screenshots' // save screenshots here
},
"globals": {
"waitForConditionTimeout": 5000 // sometimes internet is slow so wait.
},
"desiredCapabilities": { // use Chrome as the default browser for tests
"browserName": "chrome"
}
},
"chrome": {
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true // set to false to test progressive enhancement
}
}
}
}
guinea-pig.js
module.exports = { // addapted from: https://git.io/vodU0
'Guinea Pig Assert Title': function(browser) {
browser
.url('https://saucelabs.com/test/guinea-pig')
.waitForElementVisible('body')
.assert.title('I am a page title - Sauce Labs')
.saveScreenshot('ginea-pig-test.png')
.end();
}
};
The server path and chromedriver path are accurate and the most recent copy. I also have the latest version of chrome installed. Can someone please help me understand what could be the problem? Thanks!
Edit: I've also restarted the whole computer, same issue.
Try using the latest version of the Selenium standalone server v.3.0.1
If that doesn't work, then you can upgrade your chromedriver to the latest version and test. You can find the different versions here:
https://chromedriver.storage.googleapis.com/index.html
Also make sure you are using the most recent version of Nightwatch v0.9.9 and update it in you package.json file.
It is written very clear , your chrome version is lower than what chromedriver needs, just update your chrome to the latest version

Can nightwatch.js use usingServer from selenium-webdriver?

I have a suite of tests using local drivers built with nightwatch. Works well.
I am running a basic test using Perfecto Mobile and selenium-webdriver. It works with this example.(https://community.perfectomobile.com/series/20208/posts/1002862)
I am trying to connect my suite of nightwatch test to perfecto, but can't start selenium. If this is the url of the selenium server.
var url = "https://mobilecloud.perfectomobile.com/nexperience/perfectomobile/wd/hub";
I tried a few ways to set it up under nightwatch config, but can't make it work.
"selenium" : {
"start_process" : true,
"host" : "mobilecloud.perfectomobile.com/nexperience/perfectomobile/wd/hub"
}
Is there an equivalent to usingServer in nightwatch? I haven't seen anything in the docs.
driver = new webdriver.Builder().
usingServer(url).
withCapabilities(capabilities).
build();
Turns out this was the setup needed.
"test_settings" : {
"perfecto" : {
"use_ssl": true,
"default_path_prefix": "/nexperience/perfectomobile/wd/hub",
"selenium_port" : 443,
"selenium_host" : "mobilecloud.perfectomobile.com",
"desiredCapabilities": {
"browserName": "xxx",
"deviceName": "xxx",
"user": "xxx#xxx.xxx",
"password": "xxx",
"platformName": "xxx"
}
}
}
The important part that didn't work at the time of writing the question: the default_path_prefix was not customizable.
/wd/hub was the default prefix. But I really needed /nexperience/perfectomobile/wd/hub.
Two days later, this commit was introduced. It is available in 0.9.5 release.
https://github.com/nightwatchjs/nightwatch/commit/aa24c2c2334c42388318498f654d8fe2957967d1