用于测试的 Chrome 无法在 Docker 容器中启动

Chrome for Testing Unable to Start in Docker Container

提问人:HeronAlgoSearch 提问时间:8/22/2023 更新时间:8/30/2023 访问量:544

问:

我有以下Docker文件,我正在尝试与Chrome和ChromeDriver一起使用。

RUN microdnf install -y unzip
ARG CHROME_VERSION=116.0.5845.96
RUN curl -s -o  /tmp/chrome-linux64.zip https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/$CHROME_VERSION/linux64/chrome-linux64.zip \
    && unzip /tmp/chrome-linux64.zip -d /opt \
    && ln -s /opt/chrome-linux64/chrome /usr/local/bin/chrome \
    && rm /tmp/chrome-linux64.zip

## ChromeDriver

ARG CHROME_DRIVER_VERSION=116.0.5845.96
RUN curl -s -o /tmp/chromedriver_linux64.zip https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/$CHROME_DRIVER_VERSION/linux64/chromedriver-linux64.zip \
    && unzip /tmp/chromedriver_linux64.zip -d /opt/selenium \
    && rm /tmp/chromedriver_linux64.zip \
    && mv /opt/selenium/chromedriver-linux64/chromedriver /opt/selenium/chromedriver-$CHROME_DRIVER_VERSION  \
    && chmod 755 /opt/selenium/chromedriver-$CHROME_DRIVER_VERSION \
    && ln -s /opt/selenium/chromedriver-$CHROME_DRIVER_VERSION /usr/bin/chromedriver

ENV CHROMEDRIVER_PORT 4444
ENV CHROMEDRIVER_WHITELISTED_IPS "127.0.0.1"
ENV CHROMEDRIVER_URL_BASE ''
EXPOSE 4444

EXPOSE 8080
EXPOSE 5005
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
# For Testing
ENTRYPOINT ["java","-jar", "-Xmx600m","/app.jar"]

我的java代码是:

ChromeDriverService service = new ChromeDriverService.Builder()
                .withVerbose(false)
                .withSilent(true)
                .build();
        try {
            java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.WARNING);
//            service.sendOutputTo(new FileOutputStream("/dev/null"));
            service.sendOutputTo(OutputStream.nullOutputStream());
        } catch (Exception e) {
            LOGGER.error("Unable to suppress output");
        }
        return new ChromeDriver(service, getChromeOptions(useFastStrategy));

private ChromeOptions getChromeOptions(boolean useFastStrategy) {
        ChromeOptions chromeOptions = new ChromeOptions();


        chromeOptions.addArguments(String.format("user-agent=%s", USER_AGENT));
        chromeOptions.addArguments("--log-level=OFF");
        chromeOptions.addArguments("--headless=new");
        List<String> arguments = new LinkedList<>();
        arguments.add("--disable-extensions");
        // disable-infobars no longer works, see the following link for a potential answer:
        // https://stackoverflow.com/questions/57298901/unable-to-hide-chrome-is-being-controlled-by-automated-software-infobar-within
        // also see
        // https://qaautomation.expert/2022/04/01/how-to-disable-infobar-warning-for-chrome-tests-in-selenium/ . Perhaps
        // comment out 'arguments.add("disable-infobars");' in the future or try
        // chromeOptions.setExperimentalOption("excludeSwitches", Arrays.asList("enable-automation")); // <- maybe try this
        // instead of 'arguments.add("disable-infobars");'
        arguments.add("disable-infobars"); // try enabling this to try and save some cpu
        arguments.add("--headless");
        arguments.add("--disable-gpu");
        arguments.add("--no-sandbox");
        arguments.add("--incognito");
        arguments.add("--disable-application-cache");
        arguments.add("--disable-dev-shm-usage");

        chromeOptions.addArguments(arguments);
        return chromeOptions;
    }

但是我收到一个错误:

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. 
Host info: host: '22e9e505804a', ip: '172.17.0.2'
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:536)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:232)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:159)
    at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:108)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:88)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:83)
...
Caused by: org.openqa.selenium.WebDriverException: Driver server process died prematurely.
Build info: version: '4.11.0', revision: '040bc5406b'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit', java.version: '17.0.2'
Driver info: driver.version: ChromeDriver
    at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:237)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:119)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:518)
...
Caused by: org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. 
Host info: host: '22e9e505804a', ip: '172.17.0.2'
...
Caused by: org.openqa.selenium.WebDriverException: Driver server process died prematurely.
Build info: version: '4.11.0', revision: '040bc5406b'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit', java.version: '17.0.2'
Driver info: driver.version: ChromeDriver
    at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:237)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:119)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:518)
    ... 112 common frames omitted

有谁知道如何让它适用于带有 docker 容器的“Chrome for Testing”?我是否必须设置一些标志,或者我的方法不正确?

Java Docker 网络 硒-chrome驱动程序

评论


答:

2赞 Jan Kliszcz 8/30/2023 #1

错误消息表明,启动浏览器本身时可能存在一些问题。

根据 Chromium 的 Puppeteer 故障排除,您需要确保已安装 Chromium 引擎的所有依赖项。

我用于运行 ASP.NET 应用程序的图像就是这种情况。 在 Dockerfile 中添加一个步骤以从列表中安装所有必要的依赖项后,我可以毫无问题地使用 Selenium 启动 Chrome 进行测试

评论

0赞 HeronAlgoSearch 9/13/2023
是的,这是我错过的东西之一。我必须通过以下方式安装依赖项: ''' RUN yum install -y libX11 libXcomposite libXcursor libXdamage libXext libXi libXtst cups-libs libXScrnSaver libXrandr alsa-lib pango atk at-spi2-atk gtk3 '''