Issue
I was doing web scrapping using selenium java. I have the same version of chrome driver as my chrome browser and i have also set the binary path for chrome in my code:
ChromeOptions options = new ChromeOptions();
options.setBinary("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
WebDriver driver = new ChromeDriver(options);
The code in Springboot works fine, I built a docker image with Dockerfile:
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"]
and when I run the docker image, I got the error 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)
My Chrome browser version is 119.0.6045.160 and my chrome driver version is 119.0.6045.105. Any help is appreciated.
Solution
Docker containers use a different filesystem layout to your main computer.
You wrote in your code,
options.setBinary("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
But chrome.exe does not exist inside the Docker container.
It will also not help to COPY
your chrome.exe into the container, because Docker containers run Linux (Debian Linux in your case), and Linux cannot run exe files.
What you should do is use the package manager to install a copy of Chrome for the container to use. Here's what I did in my own Dockerfile to install dependencies:
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
Then you can install a Linux version of Chrome by downloading and installing Chrome from its website into the container:
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
I personally had to run Chrome with the following command line arguments:
"--no-sandbox", "--disable-setuid-sandbox", "--headless", "--disable-gpu", "--remote-debugging-port=9222"
I assume Chrome gets installed on the PATH
, so Java should be able to auto-detect the location of the Chrome binary without you having to setBinary
.
Answered By - Cadence
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.