Exception in thread "main" java.lang.Error: Unreso...
創建於:2025年12月9日
使用 GPT-5.1 Thinking 回答 Chat01
創建於:2025年12月9日
使用 GPT-5.1 Thinking 回答 Chat01
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)
That error means the Java compiler has no idea what WebDriver or ChromeDriver are – either because:
Let’s fix both.
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.
Add this to your build.gradle:
gradledependencies { testImplementation 'org.seleniumhq.selenium:selenium-java:PUT_LATEST_VERSION_HERE' }
Then refresh/reimport the Gradle project.
selenium-java-<version>.jar plus a libs folder.If the jars aren’t on the classpath, you’ll continue to see “cannot be resolved to a type” no matter what you import.
At the top of SeleniumScript.java, you should have:
javaimport org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver;
Your class should look roughly like:
javapackage 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:
javaWebDriver driver = new ChromeDriver();
then both of these must be true:
module-info.java)If your project has a module-info.java, you may also need:
javamodule 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.
org.openqa.selenium.WebDriver and org.openqa.selenium.chrome.ChromeDriverIf 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.
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)
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:
WebDriver and ChromeDriver areThat always comes down to one (or both) of:
Let’s fix this in a very concrete, do-this-exactly way.
Download Selenium Java:
Extract the zip. You’ll see something like:
selenium-java-<version>.jarlibs folder with a bunch of other .jar files.In Eclipse:
selenium-java-<version>.jarlibs folder.Clean and rebuild:
If this step is missed, you will always get "cannot be resolved to a type", no matter what imports you write.
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:
SeleniumScript.javaAt the top of the file (after the package line), you must have:
javapackage 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:
WebDriver → press Ctrl+Shift+O (Organize Imports)org.openqa.selenium.WebDriverChromeDriver.Just to sanity check, a minimal working example:
javapackage 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:
In Eclipse, if you click Run even though there are still red compilation errors, it sometimes builds a broken .class and then throws:
textException 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:
SeleniumScript.java.SeleniumScript again.Go through these in order:
selenium-java dependency addedimport org.openqa.selenium.WebDriver; presentimport org.openqa.selenium.chrome.ChromeDriver; presentIf 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.
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
Nice — that screenshot actually shows your script worked 🎉
Title: GoogleSo the old WebDriver cannot be resolved… problem is fixed.
textWARNING: 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
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.
You have two optional improvements:
Update Selenium to the latest version
selenium-java dependency to the latest release and reload the project.Or just ignore the warning for now
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.
but im using the latest slenium version
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:
Pick whichever feels best:
When Selenium ships a version that supports CDP 143, updating to that will make the warning disappear automatically. No other changes needed.
If the warning really bothers you and you don’t need the very latest Chrome:
This is overkill for learning/playing, but common in CI setups.
Most people just live with:
textWARNING: 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:
pom.xml), andI can explain exactly why they don’t line up and what combo would be “perfectly matched.”