AWS Lambda中Python+Selenium Chrome启动失败问题求助
问题描述
在AWS Lambda上运行基于Python+Selenium的函数,用于启动Chrome浏览器访问指定网站。初始几次调用完全正常,但后续调用时出现报错,尝试多种方法仍未解决。
报错信息:
Message: session not created: Chrome failed to start: exited normally. (chrome not reachable) (The process started from chrome location /opt/chrome/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
现有配置
驱动创建代码
chrome_options = Options() service = Service("/opt/chromedriver") chrome_options.binary_location = '/opt/chrome/chrome' chrome_options.add_argument("--no-sandbox") # No protection needed chrome_options.add_argument("--headless=new") # Hide the GUI chrome_options.add_argument("--single-process") # Lambda only give us only one CPU chrome_options.add_argument("--disable-dev-shm-usage") chrome_options.add_argument("--disable-extensions"); # disabling extensions chrome_options.add_argument("--disable-gpu"); # applicable to windows os only chrome_options.add_argument("--disable-infobars"); # disabling infobars chrome_options.add_argument('--remote-debugging-port=9222') web_driver = webdriver.Chrome(options=chrome_options, service=service)
Dockerfile配置
FROM public.ecr.aws/lambda/python@sha256:a8770bd841c9caa98557ef7dc590bbd2707eb5ecab13bc8edbc81bf4961dca61 AS build RUN yum install -y unzip && \ curl -Lo "/tmp/chromedriver-linux64.zip" "https://storage.googleapis.com/chrome-for-testing-public/122.0.6261.69/linux64/chromedriver-linux64.zip" && \ curl -Lo "/tmp/chrome-linux64.zip" "https://storage.googleapis.com/chrome-for-testing-public/122.0.6261.69/linux64/chrome-linux64.zip" && \ unzip /tmp/chromedriver-linux64.zip -d /opt/ && \ unzip /tmp/chrome-linux64.zip -d /opt/ FROM public.ecr.aws/lambda/python@sha256:a8770bd841c9caa98557ef7dc590bbd2707eb5ecab13bc8edbc81bf4961dca61 RUN yum install atk cups-libs gtk3 libXcomposite alsa-lib \ libXcursor libXdamage libXext libXi libXrandr libXScrnSaver \ libXtst pango at-spi2-atk libXt xorg-x11-server-Xvfb \ xorg-x11-xauth dbus-glib dbus-glib-devel -y # Copying requirements.txt and installing them COPY requirements.txt ./ RUN pip install -r requirements.txt # Copy the chrome binary and driver COPY --from=build /opt/chrome-linux64 /opt/chrome COPY --from=build /opt/chromedriver-linux64 /opt/ #Copy the python program file COPY selenium_python_lambda.py ./ #Run the handler CMD [ "selenium_python_lambda.lambda_handler" ]
requirements.txt
selenium==4.10.0 unidecode==1.3.4 bs4==0.0.1
解决方案
针对Lambda执行环境的特性,从以下几个方向调整:
移除远程调试端口参数
删除chrome_options.add_argument('--remote-debugging-port=9222')。Lambda执行容器会被复用,第一次调用占用9222端口后,后续调用无法再次绑定该端口,直接导致Chrome启动失败。无头模式下无需该参数。强制释放WebDriver资源
在Lambda handler的逻辑末尾,必须调用web_driver.quit()而非web_driver.close()。quit()会彻底终止Chrome进程和驱动服务,避免残留进程占用资源影响后续调用。示例:def lambda_handler(event, context): try: # 业务逻辑代码 web_driver.get("https://example.com") # ... finally: # 无论执行成功与否都释放资源 web_driver.quit()添加执行权限
在Dockerfile中复制Chrome和驱动文件后,添加权限设置:COPY --from=build /opt/chrome-linux64 /opt/chrome COPY --from=build /opt/chromedriver-linux64 /opt/ # 添加执行权限 RUN chmod +x /opt/chrome/chrome /opt/chromedriver避免因文件无执行权限导致Chrome无法启动。
优化Chrome启动参数
去掉可能引发稳定性问题的--single-process,同时添加适配Lambda环境的参数:# 移除--single-process chrome_options.add_argument("--disable-background-networking") chrome_options.add_argument("--disable-ipc-flooding-protection") chrome_options.add_argument("--disable-threaded-animation") chrome_options.add_argument("--disable-threaded-scrolling")升级Selenium版本
将requirements.txt中的Selenium版本升级至最新稳定版(如4.18.1),修复旧版本的兼容性问题:selenium==4.18.1 unidecode==1.3.4 bs4==0.0.1
内容的提问来源于stack exchange,提问作者S Dhas
相关产品推荐
相关产品推荐

