0

Trying to get an AWS Lambda function to run Selenium on .NET Core. Here is code:

public string FunctionHandler(ILambdaContext context)
        {
            context.Logger.LogLine("Entering function");
            try
            {
                var driver = new InternetExplorerDriver();
                context.Logger.LogLine("Navigating to URL");

                driver.Navigate().GoToUrl("http://www.google.com/");

                context.Logger.LogLine("Returning Done");
                return "Done";
            }
            catch (Exception e)
            {
                context.Logger.LogLine("Oops: " + e);
                return "Failed";
            }
        }

The error I get in the AWS console is:

OpenQA.Selenium.WebDriverException: Cannot start the driver service on http://localhost:41663/ at OpenQA.Selenium.DriverService.Start() at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute) at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters) at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities) at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities) at OpenQA.Selenium.IE.InternetExplorerDriver..ctor(InternetExplorerDriverService service, InternetExplorerOptions options, TimeSpan commandTimeout) at OpenQA.Selenium.IE.InternetExplorerDriver..ctor(InternetExplorerDriverService service, InternetExplorerOptions options) at OpenQA.Selenium.IE.InternetExplorerDriver..ctor(InternetExplorerOptions options) at OpenQA.Selenium.IE.InternetExplorerDriver..ctor() at InstagramMagic.Function.FunctionHandler(ILambdaContext context)

1
  • It is best not to use local drivers on Lambda, you would be best served by keeping a external selenium grid and then using the grid url in your script Commented Oct 21, 2017 at 15:54

1 Answer 1

1

It is possible, but so far I've only had luck getting it to work with Chrome. AWS Lambda is running a bare bones version of Amazon Linux. If you want to run something on it beyond the basics, you must package a zip file and deploy it with all of the binaries required. Unfortunately, I doubt IE will run on AWS Lambda. However, there is hope it could run on Azure's equivalent service, which uses what they call a 'Windows Container'.

You have to specify where the Chrome binary is located within Lambda's runtime file system that contains your function, which is going to be /var/task/. This is a node.js example of what you are attempting to do, but using chromedriver.

'use strict';

exports.handler = (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);

    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();
};

I actually have a runnable packaged zip of this with a video tutorial on github, with a more detailed explanation. Peak inside the zip file to get an idea how the package should be laid out. https://blackboard.github.io/lambda-selenium/

In addition, I've submitted an issue on your behalf for a runnable .net core example.

https://github.com/blackboard/lambda-selenium/issues/22

Sign up to request clarification or add additional context in comments.

2 Comments

Fantastic! I'll give it a shot. Thanks for the example!
Any luck with .net core, I am struggling with these settings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.