javadockerselenium-webdriverdockerfileremotewebdriver

Java Selenium Inside Docker image(Selenium/standalone base image)


I use selenium/standalone-chrome base image my Dockerfile is as below.

# Use the selenium/standalone-chrome base image
FROM  selenium/standalone-chrome:96.0.4664.45

USER root

# Install OpenJDK 11
RUN apt-get update && apt-get install -y openjdk-11-jdk

USER seluser

# Set the working directory
WORKDIR /app

# Copy the Java source code
COPY src /app/src
COPY test /app/test

# Copy the Selenium WebDriver JAR file
COPY lib/selenium-server-4.1.2.jar /app/lib/selenium-server-4.1.2.jar

# Compile the Java code
RUN javac -cp ".:/app/lib/*" -d /app/classes /app/src/main/*.java /app/test/systemTests/*.java

# Set the entrypoint command to run the Java application
CMD ["java", "-cp", ".:/app/lib/*:/app/classes", "main.MainClass"]

//little example from the project

package main;
public class MainClass {
    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter your choice (1 for Instagram, 2 for Facebook):");
        int choice = scanner.nextInt();
        if (choice == 1) {
            InstagramTester instagramTester = new InstagramTester();
            instagramTester.testOpenClose();
        } else if (choice == 2) {
            FacebookTester facebookTester = new FacebookTester();
            facebookTester.testOpenClose();
        } else {
            System.out.println("Invalid choice. Please enter 1 or 2.");
        }

        scanner.close();
    }
}

package systemTests;

public class InstagramTester {
    private WebDriver driver;

    public InstagramTester() {
         ChromeOptions chromeOptions = new ChromeOptions();
         chromeOptions.addArguments("--no-sandbox");
         chromeOptions.addArguments("--window-size=1920,1080");
         URL remoteUrl;
         try {
             
             remoteUrl = new URL("http://chrome:4444/wd/hub"); 
         } catch (MalformedURLException e) {
             System.err.println("Invalid Selenium Grid URL: " + e.getMessage());
             return;
         }

          this.driver = new RemoteWebDriver(remoteUrl, chromeOptions);
    }

    public void testOpenClose() {
        driver.get("https://www.instagram.com/");
        System.out.println("Page Title: " + driver.getTitle());
        closeBrowser();
    }

    public void closeBrowser() {
        // Close the browser
        driver.quit();
    }
}

inside lib i got selenium-server-4.1.2.jar

   i first use :docker build -t basicscrapping .


after image  built 

 i enter :docker run -it  -p 4444:4444 -p 7900:7900 basicscrapping   
   i enter 1 as input and after couple of minute 

I got these errors : Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. Caused by: java.io.UncheckedIOException: java.net.UnknownHostException: chrome . . Caused by: java.net.UnknownHostException: chrome Any help is appreciated.(i dont use aby built tool like maven -gradle etc for project requirements)


Solution

  • You are trying to access driver service using http://chrome:4444/wd/hub but actually in your example there is no any point where you assign a hostname to your container.

    With docker run you can use -h flag, so that your command would be:

    docker run -h chrome -it  -p 4444:4444 -p 7900:7900 basicscrapping 
    

    Or in your code change remoteUrl = new URL("http://chrome:4444/wd/hub"); to remoteUrl = new URL("http://localhost:4444/wd/hub");