I am trying to download a file from Chrome using the Selenium library in C#. It works locally, but when I deploy the Lambda function on AWS, it throws an error:
Exception Type: WebDriverException,
Message: Error starting process: /var/task/selenium-manager/linux/selenium-manager --browser "chrome" --browser-path "/opt/chrome-layer/bin/chrome-headless-shell" --language-binding csharp --output json,
StackTrace: at OpenQA.Selenium.SeleniumManager.RunCommand(String fileName, String arguments)
InnerException: An error occurred trying to start process '/var/task/selenium-manager/linux/selenium-manager' with working directory '/var/task'. Exec format error
I created a layer on AWS that contains the chrome-headless-shell and chromedriver binaries and attached it to the Lambda. The layer is compatible with arm64. Here is the code I am using:
public void DownloadFile(string downloadPath)
{
var options = GetChromeOptions(downloadPath);
using (IWebDriver driver = new ChromeDriver(options))
{
try
{
driver.Navigate().GoToUrl(_config.BaseUrl);
// other operations
}
catch (Exception ex)
{
_logger.Error(ex, "Error downloading file");
}
}
}
private ChromeOptions GetChromeOptions(string downloadPath)
{
var options = new ChromeOptions();
options.AddUserProfilePreference("download.default_directory", downloadPath);
options.AddUserProfilePreference("download.prompt_for_download", false);
options.AddUserProfilePreference("disable-popup-blocking", "true");
var chromeBinaryPath = "/opt/chrome-layer/bin/chrome-headless-shell";
options.BinaryLocation = chromeBinaryPath;
options.AddArgument("--headless");
options.AddArgument("--disable-gpu");
options.AddArgument("--no-sandbox");
options.AddArgument("--disable-dev-shm-usage");
return options;
}
RunFileFunction:
Type: AWS::Serverless::Function
Properties:
Description: Run file
Handler: System.Lambdas.RunFileFunction::FunctionHandler
Architectures:
- arm64
Layers:
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:layer:chrome-layer:3
Environment:
Variables:
CHROME_PATH: "/opt/chrome-layer/bin/chrome-headless-shell"
DRIVER_PATH: "/opt/chrome-layer/bin/chromedriver"
Events:
ScheduleEvent:
Type: ScheduleV2
Properties:
ScheduleExpression: cron(0 7 ? * 6 *)
ScheduleExpressionTimezone: Europe/London
RetryPolicy:
MaximumRetryAttempts: 0
State: ENABLED
Any help would be appreciated. Thanks in advance!