我在aws lambda中使用selenium运行python脚本。我已经创建了2层1 selenium 和1 chrome 驱动器和Headless Chromium 。执行时收到错误
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"
]
}
我用的是Python 3.8版本。已从https://github.com/adieuadieu/serverless-chrome/releases?page=1下载最新版本v1.0.0-57 headless-chromium此chromium的驱动程序版本为https://chromedriver.storage.googleapis.com/index.html?path=86.0.4240.22/
我正在使用本教程部署到lambda https://dev.to/awscommunity-asean/creating-an-api-that-runs-selenium-via-aws-lambda-3ck3本教程使用python3.6,但我想使用一个较新的版本。
在这里发布我的lambda函数
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
先谢谢你了。
2条答案
按热度按时间5sxhfpxr1#
根据这个answer Chrome需要安装额外的库才能工作。我能够让selenium在AWS lambda上工作,方法是在AWS blog post之后为lambda创建一个docker容器。
yfjy0ee72#
我在Python 3.10上遇到了同样的问题。我按照Youtube上的this教程,设法让它与Python3.7一起工作。它似乎只适用于这些特定版本,即。Python 3.7和Selenium 3.8。