Selenium WebDriver: Headless browser script execution issue

I’m having trouble running a script with a headless browser using Selenium WebDriver. Here’s the code I’m trying to use in Eclipse:

WebDriver driver = new ChromeDriver(new ChromeOptions().setHeadless(true));

// Navigate to example.com
driver.get("https://example.com");

System.out.println("Page title: " + driver.getTitle());

But I’m getting this error:

java.lang.NoClassDefFoundError: org/openqa/selenium/remote/http/HttpClient$Factory

I’ve already added the Selenium WebDriver dependency to my project. What am I missing? How can I get this headless browser script to work? Any help would be great!

I encountered a similar issue in one of my projects. I found that the error usually indicates that some dependencies were either missing or not compatible with the driver version. I solved it by ensuring all Selenium libraries were updated to version 4.x and by using the latest ChromeDriver.

I also adjusted my project configuration by adding the necessary modules such as selenium-java, selenium-chrome-driver, and selenium-http-jdk-client. Finally, I verified my ChromeOptions settings by using:

ChromeOptions options = new ChromeOptions();
options.addArguments(“–headless=new”);

This approach worked well for me.

hey there! sounds like u might be missing some dependencies. have u tried updating ur selenium version? also, make sure u have the latest chromedriver installed and in ur system PATH. if that doesnt work, try adding the httpclient dependency to ur project. hope this helps!

I’ve dealt with this exact problem before. The ‘NoClassDefFoundError’ usually points to a dependency issue. First, ensure you’re using Selenium 4.x and have all the required JARs in your classpath. Don’t forget selenium-http-jdk-client.jar - it’s crucial for the HttpClient.

Also, double-check your ChromeDriver version matches your Chrome browser. Mismatched versions can cause headaches.

If you’re using Maven, update your pom.xml with the latest Selenium dependencies. For Gradle users, check your build.gradle file.

Lastly, try running with ‘–remote-allow-origins=*’ in your ChromeOptions. This solved a similar issue for me recently.

Remember to clean and rebuild your project after making these changes. Good luck!