Can you tell me a good way to use selenium with Render.com?

Continuing with my question below, I have completed the chrome driver setup.

In the shell script that is run during deploy, “#! /usr/bin/env bash” in the shell script executed during deploy appears to cause an error

I had tried some after that, but it didn’t work, so let me ask again for advice.
The content is that when the following code (java) is executed, an error occurs at the point “WebDriver driver = new ChromeDriver(options);”.

■ source code(java)

WebDriverManager wdm = WebDriverManager.chromedriver().clearDriverCache().cachePath("/opt/render/project/.render/chrome").avoidOutputTree();
wdm.setup();

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--no-sandbox");
options.addArguments("--user-agent=" + SystemContentBean.RequestHeader.USER_AGENT);
WebDriver driver = new ChromeDriver(options);

■ error message

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. 

Probably as per the error message, but I believe it is because the “chrome driver” is not finding “chrome”.
Can you please let me know if you know how to make the “chrome driver” find “chrome”?

To be sure, the “Dockerfile” is configured as follows.

■ Dockerfile

FROM maven:3.9.6-eclipse-temurin-21 AS build
COPY . .
RUN mvn clean package -Dmaven.test.skip=true
FROM eclipse-temurin:21-alpine

# gitとgit-secretsをインストール
RUN apk update && \
    apk add bash git make && \
    apk add --upgrade grep
RUN git clone https://github.com/awslabs/git-secrets /home/alpine/git-secrets
WORKDIR /home/alpine/git-secrets
RUN make && \
    make install

# dpkgをインストール(debファイルを使ったパッケージのインストールを行うため)
# ※ apt-getは「Alpine Linux」で使えないらしいので「apk add」に修正
RUN apk add --update dpkg

# Javaアプリケーションのjarをコピー
COPY --from=build /target/xbot-nonmoral.jar xbot-nonmoral.jar

# ローカルのrender-build.shをコピー
COPY render-build.sh .

# render-build.shに実行権限を付与
# render-build.shを実行
RUN chmod +x ./render-build.sh && \
    ./render-build.sh

# seleniumの設定
ENV PATH="${PATH}:/opt/render/project/.render/chrome/opt/google/chrome/"

ENTRYPOINT ["java", "-jar", "xbot-nonmoral.jar"]

■ render-build.sh

#!/usr/bin/env bash
# exit on error
set -o errexit

CURRENT_DIR=$(pwd)

STORAGE_DIR=/opt/render/project/.render

if [ ! -d "$STORAGE_DIR/chrome" ]; then
  echo "...Downloading Chrome"
  mkdir -p $STORAGE_DIR/chrome
  cd $STORAGE_DIR/chrome
  wget -P ./ https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
  dpkg -x ./google-chrome-stable_current_amd64.deb $STORAGE_DIR/chrome
  rm ./google-chrome-stable_current_amd64.deb
  cd "$CURRENT_DIR" # Make sure we return to where we were
else
  echo "...Using Chrome from cache"
fi

Hi there,

It looks like you are combining things from Docker runtime and a Native Runtime (NodeJS, Ruby, etc.) You can’t combine these two runtimes as they are different environments. Java will need to use Docker as we don’t have a Native Runtime for Java.

This means the render-build.sh build script doesn’t apply as this is for a Native Runtime. You should install Chrome/Chromium (I’m not sure Alpine has a Chrome APK) from your Dockerfile with something like apk add chromium.

Your WebDriverManager setup would also need to change, as the cache path isn’t going to be correct (again, this a a Native Runtime path). Since you are already using Selmium, you should just use its built-in driver manager. This should work out of the box.

You may need to set the binary of Chromium in the options, but I’m unsure about this as I’ve always just used Chrome to do this. My Java Dockerfile looks something like

FROM eclipse-temurin:21 AS baseWORKDIR /serviceCOPY .mvn/ .mvnCOPY mvnw pom.xml ./RUN ./mvnw dependency:resolveCOPY src srcFROM base AS buildRUN ./mvnw clean package -DskipTestsFROM eclipse-temurin:21-jre# Install chromeRUN apt-get update && apt-get install -y wget gnupg && apt-get cleanRUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \ && echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.listRUN apt-get update && apt-get -y install google-chrome-stable && apt-get cleanWORKDIR /serviceCOPY --from=build /service/target/my-service.jar ./# Make SSH workRUN mkdir "$HOME"/.ssh && chmod 700 "$HOME"/.sshCMD ["java", "-jar", "my-service.jar"]

Hope this helps.

Regards,

Keith
Render Support, UTC+10 :australia: