admin管理员组

文章数量:1353380

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!

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!

Share Improve this question edited Mar 31 at 19:06 Hilory 2,1417 gold badges14 silver badges30 bronze badges asked Mar 31 at 17:37 ShreyaShreya 254 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Exec format error indicates that your Chrome binary is not compatible with the platform you're trying to run it on. i.e. the Chrome binary you're using has not actually been built for the arm64 architecture.

I suggest going back to the origins of that lambda layer, specifically the Chrome binaries included, to figure out why.

本文标签: netAWS Lambda Selenium Error in CWebDriverExceptionExec format error running ChromeStack Overflow