Trying to create AWS Lambda python3.11 with selenium

1k Views Asked by At

So I'm trying to create a Lambda on python3.11 that will use selenium.

I've tried creating a layer and put chromedriver adn headless-chromium in it but it fails with error 127.

I've tried downloading chromedriver with ChromeDriverManager but it also fails:

driver = webdriver.Chrome(
    service=Service(ChromeDriverManager(version="114.0.5735.90", path="/tmp").install())
)

Any ideas on how to make it work.

I've got it working with python3.7 but one of requirements I have is to use 3.11

Error when trying to install.

/bin/sh: google-chrome: command not found
...
/bin/sh: google-chrome-dev: command not found
[ERROR] AttributeError: 'NoneType' object has no attribute 'split'
Traceback (most recent call last):
  File "/var/lang/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/var/task/ur_parser.py", line 19, in <module>
    service = Service(ChromeDriverManager().install())
  File "/var/task/webdriver_manager/chrome.py", line 40, in install
    driver_path = self._get_driver_binary_path(self.driver)
  File "/var/task/webdriver_manager/core/manager.py", line 40, in _get_driver_binary_path
    file = self._download_manager.download_file(driver.get_driver_download_url(os_type))
  File "/var/task/webdriver_manager/drivers/chrome.py", line 32, in get_driver_download_url
    driver_version_to_download = self.get_driver_version_to_download()
  File "/var/task/webdriver_manager/core/driver.py", line 48, in get_driver_version_to_download
    return self.get_latest_release_version()
  File "/var/task/webdriver_manager/drivers/chrome.py", line 64, in get_latest_release_version
    determined_browser_version = ".".join(determined_browser_version.split(".")[:3])
1

There are 1 best solutions below

2
On

Try using docker images for AWS Lambda; An example project would be docker-selenium-lambda by umihico.

To add, from experience, I highly recommend you use AWS CDK to manage all the Dockerfiles and infra overall, as the lambda function gets messy very quickly. See aws-cdk-lambda-container by aws for an example.

If you mind all these complications, I would definitely test by lowering the chromium version to at least 3 versions (just a rule of thumb) or updating Selenium. By the way, would have been better if you added your Selenium version to the question.