/opt/chromedriver unexpectedly exited. Status code was 127\n
Asked Answered
T

2

7

I am running python script using selenium in aws lambda. I have created 2 layers 1 for selenium and 1 for chromedriver and headless-chromium. On execution I receive error

Response
{
  "errorMessage": "Message: Service /opt/chromedriver unexpectedly exited. Status code was: 127\n",
  "errorType": "WebDriverException",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 36, in lambda_handler\n    driver = webdriver.Chrome(options=options, executable_path=chromedriver_path, desired_capabilities=d,)\n",
    "  File \"/opt/python/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py\", line 70, in __init__\n    super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], \"goog\",\n",
    "  File \"/opt/python/lib/python3.8/site-packages/selenium/webdriver/chromium/webdriver.py\", line 89, in __init__\n    self.service.start()\n",
    "  File \"/opt/python/lib/python3.8/site-packages/selenium/webdriver/common/service.py\", line 98, in start\n    self.assert_process_still_running()\n",
    "  File \"/opt/python/lib/python3.8/site-packages/selenium/webdriver/common/service.py\", line 110, in assert_process_still_running\n    raise WebDriverException(\n"
  ]
}

I am using python version 3.8. Downloaded latest version v1.0.0-57 headless-chromium from https://github.com/adieuadieu/serverless-chrome/releases?page=1 ChromeDriver version for this chromium is https://chromedriver.storage.googleapis.com/index.html?path=86.0.4240.22/

I am using this tutorial for deploying to lambda https://dev.to/awscommunity-asean/creating-an-api-that-runs-selenium-via-aws-lambda-3ck3 This tutorial uses python3.6 but I wanted to use a newer version.

Posting my lambda function here

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def lambda_handler(event, context):
    print("Launching browser")
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument("--no-sandbox")
    options.add_argument("--log-level=1")
    options.add_argument("--start-maximized")
    options.binary_location = "/opt/headless-chromium"
    chromedriver_path = "/opt/chromedriver"
    driver = webdriver.Chrome(options=options, executable_path=chromedriver_path)

    driver.get('https://www.google.com/')

    driver.close();
    driver.quit();

    response = {
        "statusCode": 200,
        "body": "Selenium Headless Chrome Initialized"
    }

    return response

Thanks in advance.

Trematode answered 30/5, 2022 at 7:29 Comment(0)
N
0

According to this answer Chrome needs extra libraries installed to work. I was able to get selenium to work on AWS lambda by creating a docker container for the lambda following this AWS blog post.

Nonflammable answered 23/8, 2022 at 11:7 Comment(0)
G
0

I've had the same issue on Python3.10. I managed to get it working with Python3.7, following this tutorial on Youtube. It seems to be working only with these specific versions, i.e. Python3.7 and selenium 3.8.

Gyrostatics answered 11/5, 2023 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.