繁体   English   中英

为什么带有 Chrome 驱动程序的 Selenium 在本地工作但在 AWS Lambda 上崩溃?

[英]Why does Selenium with Chrome driver work locally but crashes on AWS Lambda?

我正在编写一个小应用程序来抓取 Python 中的网站,我想将 package 放入容器中并部署在 AWS Lambda 上。

我写了一个 Docker 设置,当我在本地测试它时效果很好(按照 指南)。 但是,当我在 AWS 上部署它时,Chrome 在 Selenium 启动时无法启动。 错误消息不是很有见地:

[1670327680.879][INFO]: Launching chrome: /opt/chrome/google-chrome --allow-pre-commit-input --allow-running-insecure-content --data-path=/tmp/data-path --disable-accelerated-2d-canvas --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-gpu --disable-hang-monitor --disable-ipv6 --disable-notifications --disable-popup-blocking --disable-prompt-on-repost --disable-setuid-sandbox --disable-sync --disable-web-security --disk-cache-dir=/tmp/cache-dir --enable-automation --enable-blink-features=ShadowDOMV0 --enable-logging --headless --hide-scrollbars --homedir=/tmp --ignore-certificate-errors --lang=en-GB --log-level=0 --mute-audio --no-cache --no-first-run --no-sandbox --no-service-autorun --password-store=basic --remote-debugging-port=0 --start-maximized --test-type=webdriver --use-mock-keychain --user-data-dir=/tmp/user-data --v=99 --window-size=1472,828
[1670327682.287][SEVERE]: CreatePlatformSocket() failed: Address family not supported by protocol (97)
[1670327682.287][SEVERE]: CreatePlatformSocket() failed: Address family not supported by protocol (97)
[1670327688.346][INFO]: [d53e8f7697487d8804187ea37ebb32ea] RESPONSE InitSession ERROR unknown error: Chrome failed to start: crashed.

由于它适用于我的本地测试,因此我排除了由于版本、依赖项等原因引起的所有问题。 在线查看,我发现我的本地环境与 Lambda 之间的唯一区别是文件系统权限,可能还有对 IPv6 的支持。 我试图用传递给 chrome 的选项来纠正这个问题,但没有帮助。 我也曾尝试将 chrome 安装放入/tmp目录,如另一个类似问题中所建议的那样,但它也没有用。

我正在使用此脚本安装 Chrome 和 Chrome 驱动程序:

chrome_versions=( ['109.0.5414.25']='1070081' )
chrome_drivers=( "109.0.5414.25" )

for br in "${!chrome_versions[@]}"
do
echo "Downloading Chrome version $br"
mkdir -p "/opt/chrome/$br"
curl -Lo "/opt/chrome/$br/chrome-linux.zip" "https://www.googleapis.com/download/storage/v1/b/chromium-browser-snapshots/o/Linux_x64%2F$%7Bchrome_versions%5B$br%5D%7D%2Fchrome-linux.zip?alt=media"
unzip -q "/opt/chrome/$br/chrome-linux.zip" -d "/opt/chrome/$br/"
mv /opt/chrome/$br/chrome-linux/\* /opt/chrome/
ln -s /opt/chrome/chrome /opt/chrome/google-chrome
rm -rf /opt/chrome/$br/chrome-linux "/opt/chrome/$br/chrome-linux.zip"
done

# Download Chromedriver

for dr in ${chrome_drivers[@]}
do
echo "Downloading Chromedriver version $dr"
mkdir -p "/opt/chromedriver/$dr"
curl -Lo "/opt/chromedriver/$dr/chromedriver_linux64.zip" "https://chromedriver.storage.googleapis.com/$dr/chromedriver_linux64.zip"
unzip -q "/opt/chromedriver/$dr/chromedriver_linux64.zip" -d "/opt/chromedriver/"
chmod +x "/opt/chromedriver/chromedriver"
rm -rf "/opt/chromedriver/$dr/chromedriver_linux64.zip"
done

出于测试目的,我正在启动驱动程序

from selenium import webdriver
driver = webdriver.Chrome(options=options,service_log_path='/tmp/chromedriver.log')
driver.get("https://www.selenium.dev/selenium/web/web-form.html")

您可以在此处找到整个项目 (WIP)。

令我沮丧的是,我似乎无法获得有关崩溃性质的任何信息,所以在这一点上我只是盲目猜测。 你能给我任何关于如何调试它的提示吗?

我怀疑 Lambda 运行时环境正在限制网络流量到您正在尝试同时运行的 Chrome 实例的服务。

我建议在 ECS/Fargate 上运行容器镜像,而不是在 Lambda 运行时环境中运行镜像。 虽然 Lambda 可以运行容器,但它做的事情有点不同,听起来你正在尝试做的事情更适合在你的笔记本电脑上运行容器,而不是试图让它在 Lambda 内部工作。使用 Fargate,你仍然不必担心配置底层基础设施,因此它接近 Lambda 级别的复杂性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM