我不使用木偶师,但这并不重要。
FROM public.ecr.aws/lambda/provided:al2
RUN yum install unzip atk at-spi2-atk gtk3 cups-libs pango libdrm \
libXcomposite libXcursor libXdamage libXext libXtst libXt \
libXrandr libXScrnSaver alsa-lib \
xorg-x11-server-Xvfb wget shadow-utils -y
COPY install-chrome.sh /tmp/
RUN /usr/bin/bash /tmp/install-chrome.sh
ENV DBUS_SESSION_BUS_ADDRESS="/dev/null"
我并不是100% DBUS_SESSION_BUS_ADDRESS 是必要的。我也不是 100% 确定是否有必要显式命名所有这些包,我从十几个不同的地方偷了所有东西,可能 chrome rpm 会引入它需要的东西,但我从未使用过任何基于 RHEL 的系统,所以我完全一无所知。我知道这有效。欢迎优化。
这是脚本:
#!/usr/bin/bash
# Download and install chrome
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
# Without -y it doesn't run because it needs to add dependencies.
yum install -y google-chrome-stable_current_x86_64.rpm
rm google-chrome-stable_current_x86_64.rpm
CHROMEVERSION=`wget -qO- https://chromedriver.storage.googleapis.com/LATEST_RELEASE`
wget --no-verbose -O /tmp/chromedriver_linux64.zip https://chromedriver.storage.googleapis.com/$CHROMEVERSION/chromedriver_linux64.zip
unzip /tmp/chromedriver_linux64.zip -d /opt
rm /tmp/chromedriver_linux64.zip
mv /opt/chromedriver /opt/chromedriver-$CHROMEVERSION
chmod 755 /opt/chromedriver-$CHROMEVERSION
ln -fs /opt/chromedriver-$CHROMEVERSION /usr/local/bin/chromedriver
# Create a user. /usr/sbin is not on $PATH.
/usr/sbin/groupadd --system chrome
/usr/sbin/useradd --system --create-home --gid chrome --groups audio,video chrome
您可以通过在本地启动它来验证它是否正常工作docker run --mount type=tmpfs,destination=/tmp --read-only
这很好地模拟了AWS Lambda的环境。然后你需要运行su chrome -c 'xvfb-run chromedriver --allowed-ips=127.0.0.1'
。我在用https://github.com/instaclick/php-webdriver/ https://github.com/instaclick/php-webdriver/这是一个适用于 W3C 和 Selenium 2 Webdriver 的非常瘦的 PHP 客户端。我用这个来测试:
<?php
namespace WebDriver;
require 'vendor/autoload.php';
@mkdir('/tmp/chrome');
chmod('/tmp/chrome', 0777);
$wd_host = 'http://localhost:9515';
$web_driver = new WebDriver($wd_host);
$session = $web_driver->session('chrome', [['goog:chromeOptions' => ['args' => [
'--no-sandbox',
'--user-data-dir=/tmp/chrome'
]]]]);
$session->open('https://google.com');