java Docker响应代码500,消息:未知错误:找不到Chrome二进制文件

nafvub8i  于 8个月前  发布在  Java
关注(0)|答案(1)|浏览(97)

我正在使用selenium java进行网页抓取。我有与我的Chrome浏览器相同版本的Chrome驱动程序,我还在代码中设置了Chrome的二进制路径:

ChromeOptions options = new ChromeOptions();
 options.setBinary("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");

 WebDriver driver = new ChromeDriver(options);

字符串
Springboot中的代码运行良好,我用Dockerfile构建了一个docker镜像:

FROM debian:bullseye-slim

WORKDIR /app

EXPOSE 8081

RUN apt-get update && apt-get install -y openjdk-17-jre-headless libnss3 libxcb1

COPY target/springboot-exe.jar /app/
COPY chromedriver.exe /app/

ENTRYPOINT ["java", "-jar", "/app/springboot-exe.jar"]


当我运行docker镜像时,我得到了错误no Chrome binary:

Starting ChromeDriver 114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052}) on port 23051
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
[1700010675.161][SEVERE]: bind() failed: Cannot assign requested address (99)
ChromeDriver was started successfully.
Exception in thread "Thread-5" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: no chrome binary at C:\Program Files\Google\Chrome\Application\chrome.exe
Host info: host: '32d4e279bec7', ip: '172.17.0.2'
Build info: version: '4.8.3', revision: 'e5e76298c3'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.10.16.3-microsoft-standard-WSL2', java.version: '17.0.9'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*], binary: C:\Program Files\Google\Chr..., extensions: []}}], desiredCapabilities=Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*], binary: C:\Program Files\Google\Chr..., extensions: []}}}]
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:148)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:106)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:67)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:165)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:183)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:158)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:229)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:157)
        at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:101)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:88)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:84)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:73)
        at com.linggd.demo.Duldung.run(Duldung.java:54)
        at java.base/java.lang.Thread.run(Thread.java:840)


任何帮助都是感激不尽的。

qoefvg9y

qoefvg9y1#

Docker容器使用与主计算机不同的文件系统布局。
你在代码中写道,

options.setBinary("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");

字符串
但是chrome.exe并不存在于Docker容器中。
将chrome.exe COPY到容器中也没有帮助,因为Docker容器运行Linux(在您的情况下是Debian Linux),而Linux不能运行exe文件。
你应该做的是使用包管理器安装一个Chrome的副本供容器使用。下面是我在自己的Dockerfile中安装依赖项的操作:

RUN apt-get install -y wget libgdk-pixbuf2.0-0 libnss3 libatk-bridge2.0-0 libcups2 libdrm2 libxcomposite1 libxdamage1 libxfixes3 libxrandr2 libgbm1 libxkbcommon-x11-0 libpangocairo-1.0-0 libasound2


然后,您可以通过从其网站下载并安装Chrome到容器中来安装Linux版本的Chrome:

RUN wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN apt-get install -y ./google-chrome-stable_current_amd64.deb


我个人不得不使用以下命令行参数运行Chrome:

"--no-sandbox", "--disable-setuid-sandbox", "--headless", "--disable-gpu", "--remote-debugging-port=9222"


我假设Chrome安装在PATH上,所以Java应该能够自动检测Chrome二进制文件的位置,而无需setBinary

相关问题