java-selenium 使用固定版本chrome浏览器和chromedriver,解决chrome自动升级无法与Chromedriver匹配问题

发布时间 2023-12-11 14:00:16作者: yxchun

1、获取Google chrome、chromedriver

地址:https://googlechromelabs.github.io/chrome-for-testing/

 2、将2个压缩包解压,存放到固定目录

比如我的chromedriver位置为:D:\file\jar\chromeDriver120\chromedriver.exe  

chrome位置为:D:\\soft\\chrome120\\chrome.exe

3、代码中使用固定目录的chromedriver、chrome

        // Google chrome driver位置
        String driverPath = "D:\\soft\\chrome120\\chrome.exe";
        // chrome 浏览器位置
        String browerPath = "D:\\file\\jar\\chromeDriver120\\chromedriver.exe";
        // chrome 浏览器数据存储目录位置
        String userData="--user-data-dir=C:\\Users\\liyujiao\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\";
        System.setProperty("webdriver.chrome.driver", driverPath);
        System.setProperty("webdriver.chrome.bin", browerPath);
        ChromeOptions chromeOptions = new ChromeOptions();
        // 防止403
        chromeOptions.addArguments("--remote-allow-origins=*");
        chromeOptions.addArguments(userData);

        driver = new ChromeDriver(chromeOptions);