如何配置WebDriver版本(无需本地路径设置)?Chrome适配问题
Hey there! I see you're running Chrome 65.0.3325.181 which requires ChromeDriver 2.36 or newer, but your current setup is pulling in ChromeDriver 2.32 automatically. Let's fix this without having to manually manage driver paths or downloads.
The Best Approach: Use WebDriverManager
WebDriverManager is a handy tool that handles downloading, configuring, and matching the correct ChromeDriver version to your installed Chrome browser automatically. No more manual path setup or version mismatches!
Step 1: Add WebDriverManager Dependency
First, add the dependency to your build file:
- For Maven:
<dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>5.6.0</version> <!-- Use the latest stable version available --> </dependency> - For Gradle:
implementation 'io.github.bonigarcia:webdrivermanager:5.6.0'
Step 2: Update Your Java Code
Modify your existing code to use WebDriverManager to set up ChromeDriver before initializing the WebDriver instance:
import io.github.bonigarcia.wdm.WebDriverManager; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.chrome.ChromeDriver; public class YourChromeTestClass { public static void main(String[] args) { // Auto-download and configure the correct ChromeDriver version WebDriverManager.chromedriver().setup(); ChromeOptions chromeOptions = new ChromeOptions(); WebDriver driver = new ChromeDriver(chromeOptions); // Your test logic here... driver.quit(); // Don't forget to clean up the driver instance! } }
Step 3: Verify the Fix
When you run your code again, the console should show that it's using ChromeDriver 2.36 or newer (matching your Chrome 65 version). You'll see output like:
Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port XXXXX
Optional: Pin to a Specific ChromeDriver Version
If you want to explicitly use ChromeDriver 2.36 (instead of letting it auto-detect), you can modify the setup line to:
WebDriverManager.chromedriver().version("2.36").setup();
This ensures you get exactly the version you need, no surprises.
内容的提问来源于stack exchange,提问作者Stéphane GRILLON

