Chrome无法启动:使用Docker运行测试用例时崩溃-从Chrome位置/opt/Chrome/Chrome启动的进程不再

n3schb8v  于 2023-10-16  发布在  Docker
关注(0)|答案(2)|浏览(163)

从Chrome位置/opt/Chrome/Chrome启动的进程不再运行,因此ChromeDriver假设Chrome已崩溃)使用selenium、python和Chrome驱动程序运行UI测试用例时出现错误消息)
我试图通过Docker使用Selenium Python运行测试用例,遇到了一些困难。
这里是科技跟踪信息,
selenium :4.12.0
Python版本:3.10
Chrome驱动程序:

117.0.5938.88

Chrome二进制:

117.0.5938.88

Docker文件:

FROM public.ecr.aws/lambda/python:3.9.2023.04.17.20 as build
RUN yum install -y unzip && \
    curl -Lo "/tmp/chromedriver-linux64.zip" "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/117.0.5938.88/linux64/chromedriver-linux64.zip" && \
    curl -Lo "/tmp/chrome-linux64.zip" "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/117.0.5938.88/linux64/chrome-linux64.zip" && \
    unzip /tmp/chromedriver-linux64.zip -d /opt/ && \
    unzip /tmp/chrome-linux64.zip -d /opt/

FROM public.ecr.aws/lambda/python:3.9.2023.04.17.20
RUN yum install -q 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

COPY --from=build /opt/chrome-linux64 /opt/chrome
COPY --from=build /opt/chromedriver-linux64 /opt/

COPY pyproject.toml ./
COPY src ./src

COPY ./modernca.cer ./artifactory_ng.crt
COPY ./requirementss.txt ./
COPY ./.netrc /root/.netrc
RUN chmod 600 /root/.netrc

RUN  CERT_PATH="./artifactory_ng.crt" NODE_EXTRA_CA_CERTS=${CERT_PATH} REQUESTS_CA_BUNDLE=${CERT_PATH} NO_VERIFY_SSL=true pip install -q -r requirementss.txt
RUN pip install datadog-lambda ddtrace
RUN rm -rf artifactory_ng.crt
RUN rm -rf /root/.netrc

RUN yum update -y libcurl curl python-libs python libxml2 openssl-libs openssl nss-sysinit nss nss-tools libdb-utils libdb cpio ca-certificates
COPY --from=public.ecr.aws/datadog/lambda-extension:latest /opt/extensions/ /opt/extensions

#CMD [ "datadog_lambda.handler.handler" ]
CMD [ "src.lambdas.ui_automation.handler" ]

当我在本地运行测试用例时,它工作得很好,没有任何问题,
但是在docker中运行时,相同的测试用例失败。
我在本地打开Docker,看到文件夹opt/Chrome/Chrome..
我的 selenium 驱动器

from seleniumwire.webdriver import ChromeOptions
from selenium.webdriver.chrome.service import Service

options = ChromeOptions()
options.binary_location = '/opt/chrome/chrome'
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')        
chrome_driver_service = Service(executable_path='/opt/chromedriver')
driver = webdriver.Chrome(service=chrome_driver_service,
                                      options=options
                                      )
jhdbpxl9

jhdbpxl91#

您还可以在wire模式+headless模式下使用SeleniumBase,该模式适用于任何环境。
pip install seleniumbase-并使用python运行:

from seleniumbase import Driver

driver = Driver(wire=True, headless=True)
try:
    driver.get("https://wikipedia.org")
    for request in driver.requests:
        print(request.url)
finally:
    driver.quit()

我得到的输出:

https://wikipedia.org/
https://www.wikipedia.org/
https://www.wikipedia.org/portal/wikipedia.org/assets/js/index-6852360a56.js
https://www.wikipedia.org/portal/wikipedia.org/assets/img/[email protected]
https://www.wikipedia.org/portal/wikipedia.org/assets/js/gt-ie9-ce3fe8e88d.js
https://www.wikipedia.org/portal/wikipedia.org/assets/img/sprite-8bb90067.svg
https://www.wikipedia.org/portal/wikipedia.org/assets/img/[email protected]
rqcrx0a6

rqcrx0a62#

尝试运行带有--whitelisted-ips标志的ChromeDriver(参见https://stackoverflow.com/a/57328161/1288109)。

相关问题