You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chromedriver路径正确却抛出IllegalStateException的原因排查

Mac下ChromeDriver无法验证开发者,抛出IllegalStateException问题

确认chromedriver文件路径正确(路径中模糊部分为“sName”),但运行代码时仍抛出IllegalStateException错误。奇怪的是,同一个chromedriver在其他项目(比如section4)里能正常运行,访问Flipkart并完成操作。

已尝试的操作:

  • 在终端执行命令:spctl --add --label 'Approved' chromedriver
  • 移除路径中的.exe后缀

但Mac仍然弹出提示:“无法打开chromedriver,无法验证开发者”。

相关代码如下:

flipKart.java

package section4flipkart;

import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import utils.WaitUtility;


public class flipKart {

WebDriver driver = new ChromeDriver();
    
    String url = "https://www.flipkart.com/";
    
    public void invokeBrowser() {
        System.setProperty("webdriver.chrome.driver", "/Users/sName/eclipse-workspace/libs/chromedriver.exe");
        
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.get(url);
    }
    
    public void mouseHover() {
        driver.findElement(By.xpath("//button[@class='_2KpZ6l _2doB4z']")).click();
        
        WebDriverWait wait1 = new WebDriverWait(driver, Duration.ofSeconds(10));
        wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[text()='Electronics']")));
        
        WebElement electronicsLink = driver.findElement(By.xpath("//div[text()='Electronics']"));
        
        Actions action = new Actions(driver);
        action.moveToElement(electronicsLink).build().perform();
        
        WaitUtility.waitTillElementVisible(driver, By.xpath("//a[text()='Powerbank']"), Duration.ofSeconds(10));
        WebElement powerbankLink = driver.findElement(By.xpath("//a[text()='Powerbank']"));
        action.moveToElement(powerbankLink).click().build().perform();
    }
    
    public static void main(String[] args) {
        flipKart fp = new flipKart();
        fp.invokeBrowser();
        fp.mouseHover();
    }
}

WaitUtility.java

package utils;


import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class WaitUtility {
    public static void waitTillElementVisible(WebDriver driver, By by, Duration timeoutInSeconds) {
        WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
        
        wait.until(ExpectedConditions.visibilityOfElementLocated(by));
    }
}

解决方法

1. 修正代码初始化顺序

你当前代码先实例化ChromeDriver,再设置系统属性,这会导致驱动初始化时找不到路径直接报错。必须把设置系统属性的代码放在实例化驱动之前,同时去掉Mac下chromedriver不需要的.exe后缀:

修改flipKart.java的核心代码:

public class flipKart {

    WebDriver driver; // 先声明对象,不提前初始化
    String url = "https://www.flipkart.com/";
    
    public void invokeBrowser() {
        // 先设置驱动路径,Mac下无.exe后缀
        System.setProperty("webdriver.chrome.driver", "/Users/sName/eclipse-workspace/libs/chromedriver");
        driver = new ChromeDriver(); // 再初始化驱动
        
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.get(url);
    }
    // 其他方法保持不变
}

2. 处理Mac开发者验证问题

如果修改代码后仍弹出验证提示,按以下步骤操作:

  • 打开系统设置 → 隐私与安全性
  • 弹出“chromedriver无法验证开发者”提示后,在隐私与安全性页面底部会出现对应选项,点击仍要打开
  • 再次运行代码,驱动即可正常启动

若仍无效,给chromedriver添加执行权限:
在终端进入chromedriver所在目录,执行命令:

chmod +x chromedriver

最后确认chromedriver版本与本地Chrome浏览器版本完全匹配,版本不兼容也会导致启动失败。


内容的提问来源于stack exchange,提问作者YungTiffin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 03:55:37