使用WebDriver Manager遇连接异常及Selenium驱动问题求助
解决WebDriver Manager连接异常与Selenium 4.10.0 NoSuchDriverException问题
一、WebDriver Manager连接chromedriver存储服务器失败异常
异常信息
io.github.bonigarcia.wdm.config.WebDriverManagerException: io.github.bonigarcia.wdm.config.WebDriverManagerException: org.apache.hc.client5.http.HttpHostConnectException: Connect to https://chromedriver.storage.googleapis.com:443
解决方案
配置国内镜像源
由于默认谷歌存储地址国内访问受限,指定国内镜像(如淘宝镜像)即可解决:ChromeOptions chromeOptions = new ChromeOptions(); WebDriverManager.chromedriver() .mirrorUrl("https://npm.taobao.org/mirrors/chromedriver/") .setup(); WebDriver driver = new ChromeDriver(chromeOptions);手动指定本地驱动文件
若镜像仍无法使用,直接指定本地已下载的chromedriver路径:ChromeOptions chromeOptions = new ChromeOptions(); WebDriverManager.chromedriver() .useDriverExecutable(new File("你的chromedriver文件绝对路径")) .setup(); WebDriver driver = new ChromeDriver(chromeOptions);
二、Selenium 4.10.0直接实例化ChromeDriver出现NoSuchDriverException
异常信息
org.openqa.selenium.remote.NoSuchDriverException: Unable to obtain: Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*], extensions: []}}
解决方案
确保Chrome浏览器与chromedriver版本完全匹配
- 查看Chrome版本:打开Chrome设置 → 关于Chrome,获取版本号(如114.0.5735.198)
- 下载对应版本的chromedriver,将其放在项目根目录或系统环境变量
PATH包含的路径中
显式指定驱动路径
手动设置驱动路径,避免Selenium自动下载失败:@Test public void testBrowser() { ChromeOptions options = new ChromeOptions(); options.addArguments("--remote-allow-origins=*"); System.setProperty("webdriver.chrome.driver", "你的chromedriver文件绝对路径"); WebDriver driver = new ChromeDriver(options); driver.get("https://www.google.com"); driver.quit(); }配置国内镜像给Selenium自带驱动管理器
Selenium 4.6+自带驱动管理功能,设置系统属性指定国内镜像即可自动匹配下载:@Test public void testBrowser() { System.setProperty("webdriver.chrome.driver.mirror", "https://npm.taobao.org/mirrors/chromedriver/"); ChromeOptions options = new ChromeOptions(); options.addArguments("--remote-allow-origins=*"); WebDriver driver = new ChromeDriver(options); driver.get("https://www.google.com"); driver.quit(); }
内容的提问来源于stack exchange,提问作者Sanat
相关产品推荐
相关产品推荐

