Chromedriver and Selenium WebDriver

Hi!

I was trying to use Selenium WebDriver and Chromedriver for a project. I had the following code:

require('chromedriver');
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder()
  .forBrowser('chrome')
  .build();

However, I get an error about the Chrome binary? I believe the issue has something to do with this binary not being found on the PATH, but I’m unsure how to go about resolving this. Any help would be appreciated :slight_smile: Error below:

WebDriverError: unknown error: cannot find Chrome binary
  (Driver info: chromedriver=2.46.628388 (4a34a70827ac54148e092aafb70504c4ea7ae926),platform=Linux 4.4.0-1066-aws x86_64)

    at Object.checkLegacyResponse (/rbd/pnpm-volume/5c14e12f-0790-4195-ba5f-0315077802ad/node_modules/.registry.npmjs.org/selenium-webdriver/3.6.0/node_modules/selenium-webdriver/lib/error.js:546:15)

    at parseHttpResponse (/rbd/pnpm-volume/5c14e12f-0790-4195-ba5f-0315077802ad/node_modules/.registry.npmjs.org/selenium-webdriver/3.6.0/node_modules/selenium-webdriver/lib/http.js:509:13)

    at doSend.then.response (/rbd/pnpm-volume/5c14e12f-0790-4195-ba5f-0315077802ad/node_modules/.registry.npmjs.org/selenium-webdriver/3.6.0/node_modules/selenium-webdriver/lib/http.js:441:30)

    at <anonymous>

    at process._tickCallback (internal/process/next_tick.js:189:7)

From: Task: WebDriver.createSession()

    at Function.createSession (/rbd/pnpm-volume/5c14e12f-0790-4195-ba5f-0315077802ad/node_modules/.registry.npmjs.org/selenium-webdriver/3.6.0/node_modules/selenium-webdriver/lib/webdriver.js:769:24)

    at Function.createSession (/rbd/pnpm-volume/5c14e12f-0790-4195-ba5f-0315077802ad/node_modules/.registry.npmjs.org/selenium-webdriver/3.6.0/node_modules/selenium-webdriver/chrome.js:761:15)

    at createDriver (/rbd/pnpm-volume/5c14e12f-0790-4195-ba5f-0315077802ad/node_modules/.registry.npmjs.org/selenium-webdriver/3.6.0/node_modules/selenium-webdriver/index.js:170:33)

    at Builder.build (/rbd/pnpm-volume/5c14e12f-0790-4195-ba5f-0315077802ad/node_modules/.registry.npmjs.org/selenium-webdriver/3.6.0/node_modules/selenium-webdriver/index.js:642:16)

Hi @ericyangwa, welcome to the Glitch Forums!

There isn’t a straightforward way to do this. The container your project runs in is a headless Ubuntu that doesn’t have Chrome installed. You might be able to work around this by downloading the appropriate Chrome .deb file and installing it using dpkg and the --force-no-root option.

If that’s successful then you’ll need to tell Selenium where to find Chrome, since it won’t be on the path by default. Apparently. there’s a mechanism for this.

You might also look at https://www.npmjs.com/package/selenium-standalone, but I have no idea how well that would work.

Let us know if you’re able to get this running - I think there are probably a few other community members that would like to know how to make it work as well.

Thanks @cori ! I’ll give these a try. Will have to see if I can install chrome via command line!

You’ll likely need to use a true headless browser (like PhantomJS) because the projects don’t have a display to run on. Unfortunately for this case, we don’t even have xvfb installed on the container to simulate a display.

1 Like

Yea. The project uses PhantomJS to take screenshots of a certain site, but it unfortunately has some elements that don’t load completely when I use the render function to take a ss, so I wanted to use Selenium WebDriver to analyze some css elements to determine when something has loaded before actually taking the screenshot. Not sure if this is the best way to approach it though haha.

Per the Mozilla docs it seems like perhaps you could use Firefox in a headless fashion even without installing xvfb if you’re not completely attached to Chrome, but I think you’re going to run into disk space problems there, for the time being; the Firefox install is far too large to fit in the container’s available disk space (and so is the Chrome binary, in all retrospective likelihood).

I managed to fix it, but it doesn’t really answer my original question haha. Because what I really wanted was a Phantom browser to start with, with just:

const webdriver = require('selenium-webdriver');
var capabilities = {
   'browserName' : 'phantomjs'
}

var driver = new webdriver.Builder().
  withCapabilities(capabilities).
  build();

driver... (do whatever)

worked! Thanks for all the help everyone :slight_smile:

1 Like

I still didn’t found how to use selenium, i don’t know how to make it work with chromedriver.
Can someone put some specific code please and give me accurate explanations about how to install chrome and all those things …
I always have this error

(node:558) UnhandledPromiseRejectionWarning: WebDriverError: unknown error: cannot find Chrome binary
(Driver info: chromedriver=73.0.3683.20 (8e2b610813e167eee3619ac4ce6e42e3ec622017),platform=Linux 4.4.0-1066-aws x86_64)

when i use this code

require('chromedriver');
const {Builder, By, Key, logging, until} = require('selenium-webdriver');
let driver = await new Builder()
      .forBrowser('chrome')
      .build();

and i got this error

  (unknown error: DevToolsActivePort file doesn't exist)

  (The process started from chrome location /rbd/pnpm-volume/3413e97b-1507-420a-aacc-b09a3a8055ce/node_modules/.registry.npmjs.org/chromedriver/73.0.0/node_modules/chromedriver/lib/chromedriver/chromedriver is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

  (Driver info: chromedriver=73.0.3683.20 (8e2b610813e167eee3619ac4ce6e42e3ec622017),platform=Linux 4.4.0-1066-aws x86_64)

    at Object.checkLegacyResponse (/rbd/pnpm-volume/3413e97b-1507-420a-aacc-b09a3a8055ce/node_modules/.registry.npmjs.org/selenium-webdriver/3.6.0/node_modules/selenium-webdriver/lib/error.js:546:15)

    at parseHttpResponse (/rbd/pnpm-volume/3413e97b-1507-420a-aacc-b09a3a8055ce/node_modules/.registry.npmjs.org/selenium-webdriver/3.6.0/node_modules/selenium-webdriver/lib/http.js:509:13)

    at doSend.then.response (/rbd/pnpm-volume/3413e97b-1507-420a-aacc-b09a3a8055ce/node_modules/.registry.npmjs.org/selenium-webdriver/3.6.0/node_modules/selenium-webdriver/lib/http.js:441:30)

    at <anonymous>

    at process._tickCallback (internal/process/next_tick.js:189:7)

From: Task: WebDriver.createSession()

    at Function.createSession (/rbd/pnpm-volume/3413e97b-1507-420a-aacc-b09a3a8055ce/node_modules/.registry.npmjs.org/selenium-webdriver/3.6.0/node_modules/selenium-webdriver/lib/webdriver.js:769:24)

    at Function.createSession (/rbd/pnpm-volume/3413e97b-1507-420a-aacc-b09a3a8055ce/node_modules/.registry.npmjs.org/selenium-webdriver/3.6.0/node_modules/selenium-webdriver/chrome.js:761:15)

    at createDriver (/rbd/pnpm-volume/3413e97b-1507-420a-aacc-b09a3a8055ce/node_modules/.registry.npmjs.org/selenium-webdriver/3.6.0/node_modules/selenium-webdriver/index.js:170:33)

    at Builder.build (/rbd/pnpm-volume/3413e97b-1507-420a-aacc-b09a3a8055ce/node_modules/.registry.npmjs.org/selenium-webdriver/3.6.0/node_modules/selenium-webdriver/index.js:642:16)

at create (/app/server.js:21:8)

at /app/server.js:57:5

    at Layer.handle [as handle_request] (/rbd/pnpm-volume/3413e97b-1507-420a-aacc-b09a3a8055ce/node_modules/.registry.npmjs.org/express/4.16.4/node_modules/express/lib/router/layer.js:95:5)

    at next (/rbd/pnpm-volume/3413e97b-1507-420a-aacc-b09a3a8055ce/node_modules/.registry.npmjs.org/express/4.16.4/node_modules/express/lib/router/route.js:137:13)

    at Route.dispatch (/rbd/pnpm-volume/3413e97b-1507-420a-aacc-b09a3a8055ce/node_modules/.registry.npmjs.org/express/4.16.4/node_modules/express/lib/router/route.js:112:3)

    at Layer.handle [as handle_request] (/rbd/pnpm-volume/3413e97b-1507-420a-aacc-b09a3a8055ce/node_modules/.registry.npmjs.org/express/4.16.4/node_modules/express/lib/router/layer.js:95:5)

(node:4272) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

(node:4272) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

when i use this code

require('chromedriver');
const {Builder, By, Key, logging, until} = require('selenium-webdriver');
let driver = await new Builder()
      .withCapabilities({'browserName':'chrome', "chromeOptions":{binary: require('chromedriver').path}})
      .build();