使用自动化测试套件无法启动Chrome浏览器的排查求助
Chrome浏览器无法启动问题排查(Selenium 4.11.0)
问题背景
使用自动化测试套件时Chrome浏览器无法正常启动,已执行以下操作仍未解决:
- 将Selenium版本升级至4.11.0
- 从pom.xml中移除WebDriver Manager(Selenium 4.11.0及以上版本自带WebDriver管理功能)
- 删除
~/.cache/selenium/chromedriver/linux64/路径下的旧版ChromeDriver,触发最新版本下载 - 尝试使用Chrome For Test启动浏览器
相关代码片段
case "CHROME": //System.setProperty ("webdriver.http.factory","jdk-http-client"); ChromeOptions chromeOptions = new ChromeOptions(); HashMap<String, Object> chromePrefs = new HashMap<String, Object>(); chromePrefs.put("profile.default_content_settings.popups", 0); chromeOptions.setExperimentalOption("prefs", chromePrefs); chromeOptions.addArguments("disable-infobars"); chromeOptions.addArguments("--no-sandbox"); chromeOptions.addArguments("--disable-dev-shm-usage"); chromeOptions.addArguments("--remote-allow-origins=*"); if (acr.getIncognitoModeOption().equalsIgnoreCase("Yes")) { chromeOptions.addArguments("--incognito"); } //WebDriverManager.chromedriver().driverVersion(acr.getChromeVersion()).setup(); //WebDriverManager.chromedriver().setup(); String filePath = (System.getProperty("user.dir") + "/src/main/resources/ChromeForTest/chrome-linux64/chrome").replaceAll("\\|/", "\\" + System.getProperty("file.separator")); chromeOptions.setBinary(filePath); DesiredCapabilities cap = new DesiredCapabilities(); cap.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true); cap.setCapability(ChromeOptions.CAPABILITY, chromeOptions); driver = new ChromeDriver(chromeOptions); ca = new CommonActions(); ca.writeLog("CHROME browser has launched succesfully!!");
排查与解决方案建议
验证Chrome For Test二进制文件权限
检查Chrome For Test的可执行权限,执行命令:chmod +x 你的项目根路径/src/main/resources/ChromeForTest/chrome-linux64/chrome权限不足会直接导致浏览器无法启动。
确认ChromeDriver与Chrome For Test版本完全匹配
Selenium自动下载的ChromeDriver需与你使用的Chrome For Test版本严格对应。先查看Chrome For Test版本:你的项目根路径/src/main/resources/ChromeForTest/chrome-linux64/chrome --version若版本不匹配,可手动下载对应版本的ChromeDriver放入
~/.cache/selenium/chromedriver/linux64/,或在代码中指定ChromeDriver路径:ChromeDriverService service = new ChromeDriverService.Builder() .usingDriverExecutable(new File("/path/to/matched/chromedriver")) .build(); driver = new ChromeDriver(service, chromeOptions);移除冗余的DesiredCapabilities配置
Selenium 4.x中DesiredCapabilities已被逐步废弃,直接用ChromeOptions配置即可,冗余配置可能引发冲突。删除以下代码:DesiredCapabilities cap = new DesiredCapabilities(); cap.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true); cap.setCapability(ChromeOptions.CAPABILITY, chromeOptions);改为在
ChromeOptions中直接设置:chromeOptions.setAcceptInsecureCerts(true);启用详细日志定位具体错误
添加日志配置获取ChromeDriver的详细报错信息:System.setProperty("webdriver.chrome.logfile", "/tmp/chromedriver.log"); System.setProperty("webdriver.chrome.verboseLogging", "true");查看
/tmp/chromedriver.log,可找到端口占用、依赖缺失等具体问题。验证Chrome二进制文件路径正确性
打印拼接后的路径确认有效性:System.out.println("Chrome binary path: " + filePath);避免因路径拼接错误导致找不到可执行文件。
内容的提问来源于stack exchange,提问作者Sonawane
相关产品推荐
相关产品推荐

