首页 > 解决方案 > 在 AWS 上使用 Selenium 的 Chrome 驱动程序错误:无法在 chrome 中发现打开的窗口

问题描述

我想在AWS中运行Selenium,特别是在Lambda 函数中。Lambda 函数应使用存储在Amazon ECR上的Docker Image。Selenium 代码是 Python 中的简单演示代码。

当我在本地从我的映像运行 Docker 容器时,一切正常并且 Selenium 成功,但是当我将映像部署到 AWS 并配置 lambda 函数以使用它时,它失败并抛出此错误:

selenium.common.exceptions.WebDriverException: Message: unknown error: unable to discover open window in chrome

我已经尝试了所有可能的 chrome 驱动程序参数,甚至尝试了这个问题的解决方案: Aws Lambda Ruby Crawler selenium chrome driver: 'unknown error: unable to discover open window in chrome'

似乎没有任何效果。错误不会消失。

我使用了不同的浏览器,包括具有不同版本的 google-chrome-stable、firefox、headless-chromium,都在 aws 中失败,但在本地 docker 容器中工作。

我还尝试使用所需的依赖项而不是部署 Debian 映像aws-lambda-python,但它也会引发相同的错误。

目前我正在使用 Google Chrome 版本 91、chromedriver 91.0.4472.101 用于 linux64 和 selenium 3.141.0。Dockerfile 看起来像这样:

FROM amazon/aws-lambda-python:3.8
RUN curl https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm > google-chrome-stable_current_x86_64.rpm
RUN yum install ./google-chrome-stable_current_x86_64.rpm -y
RUN pip install selenium
COPY . ./
CMD [ "main.py" ]
ENTRYPOINT [ "python" ]

主要.py:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys


if __name__ == '__main__':
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("--disable-application-cache")
    chrome_options.add_argument("--disable-infobars")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument("--hide-scrollbars")
    chrome_options.add_argument("--enable-logging")
    chrome_options.add_argument("--single-process")
    chrome_options.add_argument("--ignore-certificate-errors")
    chrome_options.add_argument("--homedir=/tmp")
    chrome_options.add_argument("--log-level=0")
    driver = webdriver.Chrome(executable_path="/var/task/chromedriver", options=chrome_options)
    driver.get("http://www.python.org")
    assert "Python" in driver.title
    elem = driver.find_element_by_name("q")
    elem.clear()
    elem.send_keys("pycon")
    elem.send_keys(Keys.RETURN)
    assert "No results found." not in driver.page_source
    driver.close()
    print("done")

目录结构

编辑 1:我尝试过使用 selenium/standalone-chrome 和 selenium/standalone-firefox 图像,就像 Moshe Slavin 建议的那样。在 chrome 上,我现在使用相同的选项(页面而不是窗口)遇到不同的错误:

selenium.common.exceptions.WebDriverException: Message: unknown error: unable to discover open pages

在 Firefox 上,我收到此错误。使用 selenium 图像之前完全相同的错误:

selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status signal

标签: amazon-web-servicesdockerseleniumselenium-webdriveraws-lambda

解决方案


推荐阅读