Home > Software design >  Selenium Java 4.1.3 java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
Selenium Java 4.1.3 java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver

Time:04-26

I followed this tutorial https://www.javatpoint.com/selenium-webdriver-installation but I have a problem with the WebDriver class :

Error: Unable to initialize main class First
Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver

I've put the dependencies as shown in the tutorial. And if I trust the documentation of Selnium API, the WebDriver class is present in the 4.1.3 version.

I don't know if it's because the 4.1.3 version don't have the client-combiden-3.13.0.jar file or something like that... So if you have a solution, I'm in.

I'm on Windows 10, and I use Eclipse IDE version 2021-06 (4.20.0).

And this is my class First :

import org.openqa.selenium.By;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.chrome.ChromeDriver;  

public class First {  
  
    public static void main(String[] args) {

    // declaration and instantiation of objects/variables  
    System.setProperty("webdriver.chrome.driver", "/root/drivers/chromedriver.exe");  
    WebDriver driver = new ChromeDriver();  

    // Launch website
    driver.navigate().to("http://www.google.com/");  

    // Click on the search text box and send value  
    driver.findElement(By.id("lst-ib")).sendKeys("javatpoint tutorials");  
 
    // Click on the search button  
    driver.findElement(By.name("btnK")).click();  

    }
}

Thank you for your help!

CodePudding user response:

You probably only added the following JARS:

  • selenium-api-4.1.3.jar
  • selenium-chrome-driver-4.1.3.jar
  • (all in 'lib')

ChromeDriver inherits from ChromiumDriver which again inherits from RemoteWebDriver who finally implements WebDriver. Since you did not provide these links, the compiler cant know that ChromeDriver implements WebDriver.

You at least need to add these external JARS:

  • selenium-api-4.1.3
  • selenium-remote-driver-4.1.3
  • selenium-chromium-driver-4.1.3
  • selenium-chrome-driver-4.1.3
  • (all in the folder 'lib')

Note: you might want to add the xxx-sources.jar too. It is not necessary but you can attach it to the compiled classes to see actual code instead of the weird representation eclipse provides in the "Class File Editor".

You can also add all jar files from the downloaded ZIP to prevent similar errors in the future. Or my preferred way: Look into Maven (https://www.vogella.com/tutorials/EclipseMaven/article.html). It manages dependencies for you, is pretty easy to use and you can update your libraries much easier. You seem to be a new programmer and I know it can look intimidating to get to know all these tools, but Maven will make your life quite a bit easier.

  • Related