Driver Cannot be Resolved in Selenium: How to Fix it

To fix this error, ensure that WebDriver is properly defined

Reading time icon 3 min. read


Readers help support Windows Report. We may get a commission if you buy through our links. Tooltip Icon

Read our disclosure page to find out how can you help Windows Report sustain the editorial team. Read more

driver cannot be resolved
A message from our partner

To fix various driver issues on your PC, you will need a dedicated tool to find the freshest and the original drivers. You can use PC HelpSoft Driver Updater to do it in 3 easy steps:

  • Download PC HelpSoft Driver Updater and install it on your PC
  • Start the scanning process to search for outdated or missing drivers that cause problems
  • Right-click on Update Now to allow the update process.
Download now PC HelpSoft has been downloaded by 0 readers this month

Encountering the driver cannot be resolved error in Selenium with Java can be quite frustrating, especially when you’re in the middle of writing or running your tests. Let’s walk through some effective solutions to get you back on track.

How can I fix the Driver cannot be resolved error?

Before we try more complex solutions, try restarting your IDE.

1. Define WebDriver at class level

  1. Ensure you are editing the correct Java file for your Selenium test.
  2. Locate the method where you attempted to initialize the WebDriver.
  3. Move the declaration of WebDriver to the class level, before the method definitions.
  4. Example:
    public class FirstTestNGFile {
        WebDriver driver;  // Declaration moved here

        @BeforeTest
        public void setup() {
            driver = new ChromeDriver();
        }
        // Other methods remain the same
        }

    webdriver declaration
  5. Save the file and rerun your test.

By moving the WebDriver declaration to the class level, it ensures the driver variable is accessible in all methods of that class. This resolves the scope issue causing the driver cannot be resolved error.

2. Check your imports

  1. Open your Java file.
  2. Ensure you have the correct import statements for Selenium WebDriver at the top of your file.
  3. Required imports typically include:
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;

    import selenium

3. Add Selenium JARs to the classpath

  1. Open Eclipse or any other Java IDE you use.
  2. Select Properties from the context menu.
  3. Click on Java Build Path from the left-hand menu.
  4. Switch to the Libraries tab.
  5. Click Add External JARs… and navigate to the directory where you downloaded the Selenium JAR files.
    add external jars
  6. Add selenium-server-standalone.jar or individual JAR files for WebDriver.

4. Verify WebDriver binary location

  1. Download the correct WebDriver executable (e.g., chromedriver for Chrome) from the official website.
  2. Add a line in your setup method to set the system property for the WebDriver.
  3. Example:
    System.setProperty("webdriver.chrome.driver", "path_to_chromedriver");
    WebDriver driver = new ChromeDriver();

    setproperty chromedriver

5. Clean and rebuild project

  1. Open your project in your Java IDE.
  2. In Eclipse, go to Project in the menu.
  3. Select Clean.
    clean eclipse
  4. Choose your project and click OK.
  5. After cleaning, rebuild your project to remove any lingering compilation errors.

6. Check the Java compiler version

  1. Right-click on your project and select Properties.
  2. Navigate to Java Compiler.
    compiler eclipse
  3. Ensure it’s set to a version compatible with your Selenium library (e.g., Java 8 or later).
  4. Apply the settings and rebuild the project.

Matching the Java compiler version with your Selenium dependencies ensures compatibility and resolves potential conflicts.

By following these solutions, you should be able to resolve the driver cannot be resolved error in Selenium with Java.

We covered a similar error in our WebDriver cannot be resolved to a type guide, so donโ€™t miss it. If youโ€™re using an Edge driver, you might get a Microsoft Edge WebDriver unknown error, but we have an article that covers that issue.

More about the topics: Browser errors

User forum

0 messages