1
votes

When I run my nodejs 12.x code calling selenium, I am getting the error below.

At the top level of my directory structure, I have my index.js file, a lib folder and a node_modules folder.

I am preparing the code on a Mac and then zipping it, uploading to S3 and running it in Lambda.

Where should my chrome and chromedriver executables be? I don't think I can use npm to install them as I think I need the linux versions for running in Lambda?

This is the error I am getting:

{ "errorType": "Error", "errorMessage": "The ChromeDriver could not be found on the current PATH. Please download the latest version of the ChromeDriver from http://chromedriver.storage.googleapis.com/index.html and ensure it can be found on your PATH.", "trace": [ "Error: The ChromeDriver could not be found on the current PATH. Please download the latest version of the ChromeDriver from http://chromedriver.storage.googleapis.com/index.html and ensure it can be found on your PATH.", " at new ServiceBuilder (/var/task/node_modules/selenium-webdriver/chrome.js:232:13)", " at getDefaultService (/var/task/node_modules/selenium-webdriver/chrome.js:321:22)", " at Function.createSession (/var/task/node_modules/selenium-webdriver/chrome.js:695:44)", " at createDriver (/var/task/node_modules/selenium-webdriver/index.js:155:33)", " at Builder.build (/var/task/node_modules/selenium-webdriver/index.js:662:16)", " at Runtime.exports.handler (/var/task/index.js:38:26)", " at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)" ] }

This is my code

'use strict';

exports.handler = async (event, context, callback) => {
    var webdriver = require('selenium-webdriver');    
    var chrome = require('selenium-webdriver/chrome');
    var builder = new webdriver.Builder().forBrowser('chrome');
    var chromeOptions = new chrome.Options();
    const defaultChromeFlags = [
        '--headless',
        '--disable-gpu',
        '--window-size=1280x1696', // Letter size
        '--no-sandbox',
        '--user-data-dir=/tmp/user-data',
        '--hide-scrollbars',
        '--enable-logging',
        '--log-level=0',
        '--v=99',
        '--single-process',
        '--data-path=/tmp/data-path',
        '--ignore-certificate-errors',
        '--homedir=/tmp',
        '--disk-cache-dir=/tmp/cache-dir'
    ];

    chromeOptions.setChromeBinaryPath("/var/task/lib/chrome");
    chromeOptions.addArguments(defaultChromeFlags);

    builder.setChromeOptions(chromeOptions);

    //*****this is the problem line****//
    var driver = builder.build(); 

    driver.get(event.url);
    driver.getTitle().then(function(title) {

        console.log("Page title for " + event.url + " is " + title)
        callback(null, 'Page title for ' + event.url + ' is ' + title);
    });

    driver.quit();
};
2

2 Answers

0
votes

Check this:

https://github.com/blackboard/lambda-selenium/blob/master/README.md

Welcome to the lambda-selenium project! The purpose of this project is to show how to use Selenium Webdriver with Amazon Web Services (AWS) Lambda compute service.

or

https://github.com/smithclay/lambdium/blob/master/README.md

Lambdium uses Selenium Webdriver with Headless Chromium to run Webdriver scripts written in JavaScript on AWS Lambda.

0
votes

You can use chrome-aws-lambda package for running it headless in your lambda function , As per Documentation 512 MB of ram will be used https://www.npmjs.com/package/chrome-aws-lambda