AWS Lambda, Chrome, Chromedriver selenium.common.exceptions.WebDriverException: "unable to discover



如有任何帮助,不胜感激。我真的觉得我很接近……然而到目前为止。叹息。

我使用下面的dockerfile创建了一个docker容器。

我在桌面上运行docker容器,它工作正常。

我正在使用:

Selenium版本:4.2.0无头chrome = 102.0.5005.61

然而,当我上传到ECR,然后部署到AWS Lambda作为容器时,我得到以下错误消息:

类"selenium.common.exceptions.WebDriverException"消息:未知错误:无法发现chrome中打开的窗口

"

FROM public.ecr.aws/lambda/python:3.9.2022.05.31.17-x86_64
ADD app.py .
ADD ChromeProfile  /tmp/ChromeProfile
ADD screenshots  /tmp/screenshots
RUN pip install selenium webdriver_manager chromedriver-binary-auto
RUN yum -y update
RUN yum clean all 
RUN rm -r /var/cache/yum
RUN yum -y install wget
RUN yum -y install gnupg
RUN yum -y install unzip
RUN yum whatprovides /usr/sbin/useradd
RUN yum install -y https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
RUN wget -N http://chromedriver.storage.googleapis.com/102.0.5005.61/chromedriver_linux64.zip -P ~/
RUN unzip ~/chromedriver_linux64.zip -d ~/
RUN rm ~/chromedriver_linux64.zip
RUN mv -f ~/chromedriver /usr/local/bin/chromedriver
RUN chown root:root /usr/local/bin/chromedriver
RUN chmod 0755 /usr/local/bin/chromedriver
RUN mkdir -p /etc/opt/chrome/policies/managed/
ADD Manifest.json /etc/opt/chrome/policies/managed/
RUN chmod -R 775 /etc/opt/chrome/policies/managed
CMD [ "app.handler" ]

"

这是我的python代码:

"

def handler(event, context): 
try:      

chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--headless')
chromeOptions.add_argument('--no-sandbox')
chromeOptions.add_argument('--start-maximized')
chromeOptions.add_argument('--start-fullscreen')
chromeOptions.add_argument('--single-process')
chromeOptions.add_argument('--disable-dev-shm-usage')
chromeOptions.add_argument('--user-data-dir=/tmp/ChromeProfile')
chromeOptions.add_argument('--profile-directory=Profile 2')
chromeOptions.binary_location = '/opt/google/chrome/chrome'

driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver', options=chromeOptions) <=== Error message shows up here.
GetChromeInfo(driver)

URLString = "http://www.google.com"

"

我试过玩各种各样的chrome选项。似乎找不到合适的魔法组合。它总是在我的桌面上工作,但在AWS Lambda中失败。

我需要使用配置文件,因为弹出询问"您想要应用程序代替吗?">

我用Alpine进行了尝试,并构建了一个加载了chromium驱动程序和浏览器的python Alpine图像。我将容器部署到ECR并将其下载到Lambda。

生活是美好的。它工作。

关于AWS Linux 2不能启动chrome的一些事情。我尝试了无数种组合。如果有人知道我做错了什么,请出于好奇告诉我。我敢肯定这是一件既简单又愚蠢的事。

相关内容

最新更新