Exception in thread "main" java.lang.Error: Unreso...

Tạo vào: 9 tháng 12, 2025

Trả lời bằng GPT-5.1 Thinking bởi Chat01

Câu hỏi

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
WebDriver cannot be resolved to a type
ChromeDriver cannot be resolved to a type

at siano.SeleniumScript.main(SeleniumScript.java:13)

Suy nghĩ

【turn0calculator0】 1+1 = 2

undefined

Suy nghĩ

【turn0calculator0】 1+1 = 2

Thought for 9s

Trả lời

That error means the Java compiler has no idea what WebDriver or ChromeDriver are – either because:

  1. The Selenium libraries aren’t on your classpath, and/or
  2. You’re missing the correct import statements.

Let’s fix both.


1. Make sure Selenium is added to your project

If you’re using Maven

Add the Selenium dependency to your pom.xml (use the latest version from Maven Central):

xml
<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>PUT_LATEST_VERSION_HERE</version> </dependency> </dependencies>

After editing pom.xml, reload the Maven project so the jars are added to the classpath.

To get the latest version: search for “selenium-java maven central” and copy the current version string into PUT_LATEST_VERSION_HERE.


If you’re using Gradle

Add this to your build.gradle:

gradle
dependencies { testImplementation 'org.seleniumhq.selenium:selenium-java:PUT_LATEST_VERSION_HERE' }

Then refresh/reimport the Gradle project.


If you’re NOT using a build tool (plain Eclipse/IntelliJ project)

  1. Download the Selenium Java zip from the official Selenium site.
  2. Extract it – you’ll see selenium-java-<version>.jar plus a libs folder.
  3. In your IDE, add all those jars to your project’s Build Path / Module dependencies.

If the jars aren’t on the classpath, you’ll continue to see “cannot be resolved to a type” no matter what you import.


2. Add the correct imports in your code

At the top of SeleniumScript.java, you should have:

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

Your class should look roughly like:

java
package siano; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class SeleniumScript { public static void main(String[] args) { // Optional in Selenium 4.6+ with Selenium Manager, but often used: // System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); WebDriver driver = new ChromeDriver(); driver.get("https://www.google.com"); System.out.println("Title: " + driver.getTitle()); driver.quit(); } }

If line 13 is:

java
WebDriver driver = new ChromeDriver();

then both of these must be true:

  • Selenium jars are on the classpath ✅
  • Those two import lines are present ✅

3. Double-check module system (if you use module-info.java)

If your project has a module-info.java, you may also need:

java
module your.module.name { requires selenium.api; // or similar, depending on how Selenium is packaged in your setup }

But most beginner setups don’t use modules, just the classpath.


Quick checklist

  • Added Selenium dependency (Maven/Gradle) or jars to build path
  • Imported org.openqa.selenium.WebDriver and org.openqa.selenium.chrome.ChromeDriver
  • Rebuilt / reloaded project

If you paste your SeleniumScript.java and how your project is set up (Maven/Gradle/Eclipse plain project), I can point out the exact line(s) to change.

Câu hỏi

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
WebDriver cannot be resolved to a type
ChromeDriver cannot be resolved to a type

at siano.SeleniumScript.main(SeleniumScript.java:11)

Suy nghĩ

Providing next steps and debugging guidance

The user likely still needs more detailed help with their Java compilation error. I can give potential fixes, like checking imports, dependencies, or the classpath setup. I should recommend verifying that they created a Java project and added the necessary jars. Also, I’ll provide steps for Eclipse, IntelliJ, and command-line setups.

Tailoring Eclipse setup and troubleshooting steps

For Eclipse,

Thought for 7s

Trả lời

Gotcha — that stack trace is Eclipse’s way of saying:

“I tried to run your program even though it didn’t compile, and I bailed out when I hit WebDriver / ChromeDriver.”

So we know two things:

  1. Your code is compiling with errors still present
  2. The compiler does not know what WebDriver and ChromeDriver are

That always comes down to one (or both) of:

  • Selenium is not on the classpath, and/or
  • The import statements are missing.

Let’s fix this in a very concrete, do-this-exactly way.


1. Make sure Selenium is actually added to the project

A. If you’re using a plain Eclipse Java project (no Maven/Gradle)

  1. Download Selenium Java:

    • Search for “Selenium Java download”.
    • Download the .zip for Selenium Java.
  2. Extract the zip. You’ll see something like:

    • selenium-java-<version>.jar
    • a libs folder with a bunch of other .jar files.
  3. In Eclipse:

    • Right-click your project → Build PathConfigure Build Path…
    • Go to the Libraries tab.
    • Click Add External JARs…
    • Add:
      • selenium-java-<version>.jar
      • all the jars inside the libs folder.
    • Click Apply and Close.
  4. Clean and rebuild:

    • ProjectClean… → select your project → OK.

If this step is missed, you will always get "cannot be resolved to a type", no matter what imports you write.


B. If you’re using Maven

Make sure your pom.xml has this (version can be adjusted to latest):

xml
<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.26.0</version> <!-- example --> </dependency> </dependencies>

Then in Eclipse:

  • Right-click project → MavenUpdate Project…
  • Check your project → OK.

2. Check your imports at the top of SeleniumScript.java

At the top of the file (after the package line), you must have:

java
package siano; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver;

If import org.openqa.selenium.WebDriver; and import org.openqa.selenium.chrome.ChromeDriver; aren’t there (or are greyed out with an error), Eclipse will complain.

You can also let Eclipse add them:

  • Put the cursor on WebDriver → press Ctrl+Shift+O (Organize Imports)
  • Choose org.openqa.selenium.WebDriver
  • Same for ChromeDriver.

3. Make sure your class looks roughly like this

Just to sanity check, a minimal working example:

java
package siano; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class SeleniumScript { public static void main(String[] args) { // For Selenium 4.6+ with Selenium Manager, this is often optional, but OK to keep: // System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); // line 11 in your case? driver.get("https://www.google.com"); System.out.println("Page title: " + driver.getTitle()); driver.quit(); } }

If line 11 in your file is the WebDriver driver = new ChromeDriver(); line, then that line will only compile if:

  • Selenium jars are on the classpath ✅
  • Those two imports are present ✅

4. Why you see this at runtime instead of as a normal compile error

In Eclipse, if you click Run even though there are still red compilation errors, it sometimes builds a broken .class and then throws:

text
Exception in thread "main" java.lang.Error: Unresolved compilation problems: WebDriver cannot be resolved to a type ChromeDriver cannot be resolved to a type

So once you fix the classpath + imports:

  1. Make sure there are no red X markers on your project or SeleniumScript.java.
  2. Then run SeleniumScript again.

Quick checklist

Go through these in order:

  • Selenium jars added to Build Path, or Maven selenium-java dependency added
  • import org.openqa.selenium.WebDriver; present
  • import org.openqa.selenium.chrome.ChromeDriver; present
  • Project cleaned / rebuilt, no red X errors in Eclipse
  • Run again

If you want, paste your SeleniumScript.java file and (1) whether you use Maven or not and (2) a screenshot/text of your project structure (especially the libraries section), and I can point at exactly what’s missing.

Câu hỏi

image.png

Dec 10, 2025 4:25:09 AM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
WARNING: Unable to find an exact match for CDP version 143, returning the closest version; found: 142; Please update to a Selenium version that supports CDP version 143
Title: Google

Suy nghĩ

Explaining the Selenium warning and safe actions

The console output showing "Title: Google" means the test ran successfully. The warning is just related to changes in how Selenium handles DevTools (since version 4.23ish). Basic WebDriver commands are fine. The warning can be ignored for basic tests, but if using DevTools features, updating Selenium might be useful. Checking the version can be done by looking at the Maven POM or jar files.

Thought for 15s

Trả lời

Nice — that screenshot actually shows your script worked 🎉

  • Chrome opened
  • Went to Google
  • Printed: Title: Google

So the old WebDriver cannot be resolved… problem is fixed.


What the new message means

text
WARNING: Unable to find an exact match for CDP version 143, returning the closest version; found: 142; Please update to a Selenium version that supports CDP version 143
  • CDP = Chrome DevTools Protocol.
  • Your Chrome is using version 143 of CDP.
  • The Selenium version you have only knows up to 142, so it picks the closest one and warns you.

Is this an error?

No — it’s just a warning.
For normal WebDriver stuff (open page, click, type, read title, etc.) you can safely ignore it. Your run output proves everything executed fine.

This mismatch only really matters if you use DevTools features in Selenium (network interception, console logs, performance tracing, etc.). Then some APIs might be slightly off.


What you can do (optional)

You have two optional improvements:

  1. Update Selenium to the latest version

    • If you’re using Maven/Gradle, bump the selenium-java dependency to the latest release and reload the project.
    • If you added jars manually, download the newest Selenium Java zip and replace the old jars in Referenced Libraries.
  2. Or just ignore the warning for now

    • For learning basic Selenium and doing simple browser automation, this warning is harmless.

So: your setup is correct now; Selenium is running.
If you’d like, next we can add a simple assertion, locate elements, or build a small test case on top of this script.

Câu hỏi

but im using the latest slenium version

Suy nghĩ

【turn0calculator0】 1+1 = 2

Thought for 10s

Trả lời

Right, that warning is confusing when you feel like you’re fully up-to-date 😅
The short version: your Selenium is new, but your Chrome is even newer.

Selenium’s CDP support has to be updated after Google ships each new Chrome/DevTools version. There’s always a little lag. When your Chrome jumps to a CDP version (143 in your case) that Selenium doesn’t yet know about, you get:

Unable to find an exact match for CDP version 143, returning the closest version; found: 142

That’s Selenium saying:

“I don’t have a CDP binding for 143 yet, I’ll use 142 instead.”

So both of these can be true at the same time:

  • You installed the latest Selenium release currently available ✅
  • Chrome on your machine is on a brand-new version that Selenium hasn’t fully caught up with yet ⚠️

What this means for you

  • Your script is working. It opened Google and printed the title, so WebDriver is fine.
  • The warning only matters if you’re using DevTools-based features (network interception, performance logs, etc.).
  • For normal automation (finding elements, clicking, typing, navigation), you can safely ignore this warning.

Options if you want the warning gone

Pick whichever feels best:

1. Wait for the next Selenium release (recommended)

When Selenium ships a version that supports CDP 143, updating to that will make the warning disappear automatically. No other changes needed.


2. Temporarily match Chrome to Selenium

If the warning really bothers you and you don’t need the very latest Chrome:

  • Install a slightly older Chrome version (or Chrome for Testing build) whose CDP version Selenium already supports.
  • Or run tests in a Docker image or automation environment where Chrome and Selenium versions are known to match.

This is overkill for learning/playing, but common in CI setups.


3. Just ignore it (totally fine)

Most people just live with:

text
WARNING: Unable to find an exact match for CDP version 143...

and keep coding. If you start using advanced DevTools stuff later and see odd behavior, then we can worry about strict version alignment.


If you tell me:

  • Your Selenium version (e.g., from your jar name or Maven pom.xml), and
  • Your Chrome version (Help → About Google Chrome),

I can explain exactly why they don’t line up and what combo would be “perfectly matched.”

Chia sẻ Q&A này