Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Selenium Webdriver on Heroku?

I am developing a Node.js app, and I use Selenium Webdriver on it for scraping purposes. However, when I deploy on Heroku, Selenium doesn't work. How can I make Selenium work on Heroku?

like image 313
Athanasios Canko Avatar asked Mar 17 '17 14:03

Athanasios Canko


People also ask

Can you run Selenium on Heroku?

Heroku CI supports testing with Selenium via the Chrome buildpacks mentioned above. Additionally, to run Selenium in Heroku CI, you'll need: Chromedriver - Your language or framework may already install this, if not, please see our Chromedriver buildpack.

What is the Selenium Grid?

What is Selenium Grid? Selenium Grid is a smart proxy server that makes it easy to run tests in parallel on multiple machines. This is done by routing commands to remote web browser instances, where one server acts as the hub. This hub routes test commands that are in JSON format to multiple registered Grid nodes.


2 Answers

Below is a javaScript sample code using selenium-webdriver npm package with chrome browser.

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

let options = new chrome.Options();
//Below arguments are critical for Heroku deployment
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--no-sandbox");

let driver = new webdriver.Builder()
  .forBrowser('chrome')
  .setChromeOptions(options)
  .build();

driver.get('http://www.google.com');
driver.quit();

Before you are ready to deploy, you would need to add two buildpacks to Heroku.

  • Using Heroku buildpacks command:
$ heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-chromedriver
$ heroku buildpacks:add --index 2 https://github.com/heroku/heroku-buildpack-google-chrome

or

  • Config in Heroku dashboard:
    Settings -> Add buildpacks -> https://github.com/heroku/heroku-buildpack-chromedriver -> Save changes
    Settings -> Add buildpacks -> https://github.com/heroku/heroku-buildpack-google-chrome -> Save changes
like image 67
Angela L. Avatar answered Sep 27 '22 20:09

Angela L.


I was able to get the Selenium Webdriver working on Node/Heroku using PhantomJs as the headless browser. I installed the PhantomJs buildpack to my Heroku app and it just worked. I struggled to get Chrome and Firefox drivers working on Heroku... I wrote a blog with the steps and code I used to get it working:

http://www.viderman.com/2017/05/selenium-on-heroku.html

like image 21
Alex Viderman Avatar answered Sep 27 '22 20:09

Alex Viderman