解决Java Selenium连接Incogniton反指纹浏览器已打开会话失败问题
Java Selenium连接Incogniton反指纹浏览器的问题解决
问题背景
我需要用Java Selenium连接Incogniton反指纹浏览器的已打开会话,但该工具官方仅支持Python版Selenium自动化,参照教程写代码后运行报错。
原代码
import org.openqa.selenium.chrome.ChromeOptions; import java.io.IOException; public class selenium { public static void main(String[] args) throws InterruptedException, IOException { System.setProperty("webdriver.chrome.driver", "C:\\Users\\test\\browser\\117\\win\\chrome.exe"); // 此处填的是Incogniton的Chrome程序路径 ChromeOptions options = new ChromeOptions(); options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222"); WebDriver driver = new ChromeDriver(options); driver.get("https://google.com"); System.out.println(driver.getTitle()); }}
报错信息
[4540:3448:0927/210547.993:ERROR:policy_logger.cc(154)] :components\enterprise\browser\controller\chrome_browser_cloud_management_controller.cc(163) Cloud management controller initialization aborted as CBCM is not enabled.
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
问题分析与解决步骤
1. 修正ChromeDriver路径错误
原代码把**Chrome浏览器程序(chrome.exe)**当成了ChromeDriver,这是核心错误。必须指向Incogniton配套的chromedriver.exe,路径示例:C:\Users\test\browser\117\win\chromedriver.exe(根据实际路径调整)。
2. 确认Incogniton的远程调试端口已开启
- 打开Incogniton,进入对应浏览器配置的「启动参数」设置,添加
--remote-debugging-port=9222 - 或启动会话时,确保开启「远程调试」选项(部分版本在会话启动界面有该开关)
- 验证端口可用性:浏览器访问
http://127.0.0.1:9222,能看到调试页面则端口正常
3. 补全代码缺失的依赖导入
原代码缺少WebDriver和ChromeDriver的导入,需添加:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver;
4. 确保版本匹配
- Java Selenium版本要和ChromeDriver版本一致
- ChromeDriver版本要和Incogniton内置的Chrome版本(117)对应
修正后的完整代码
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import java.io.IOException; public class IncognitonSelenium { public static void main(String[] args) throws InterruptedException, IOException { // 配置正确的ChromeDriver路径 System.setProperty("webdriver.chrome.driver", "C:\\Users\\test\\browser\\117\\win\\chromedriver.exe"); ChromeOptions options = new ChromeOptions(); // 连接已开启的Incogniton调试会话 options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222"); WebDriver driver = new ChromeDriver(options); // 获取当前页面标题(不要用get跳转,会覆盖已有会话页面) System.out.println(driver.getTitle()); // 示例:执行页面操作 // driver.findElement(By.id("target-id")).click(); // 不要主动关闭,避免影响Incogniton会话 // driver.quit(); } }
Java Selenium控制Incogniton的两种方式
方式1:连接已打开的会话(如上)
核心是通过debuggerAddress绑定已开启远程调试的Incogniton会话,适合复用已登录的环境。
方式2:启动新的Incogniton会话
如果需要直接启动新会话,需配置ChromeOptions指向Incogniton的Chrome程序路径,并添加反指纹相关参数:
ChromeOptions options = new ChromeOptions(); // 指定Incogniton的Chrome程序路径 options.setBinary("C:\\Users\\test\\browser\\117\\win\\chrome.exe"); // 添加Incogniton默认的反指纹启动参数(可从Incogniton配置中复制) options.addArguments("--disable-blink-features=AutomationControlled"); options.addArguments("--no-first-run"); // 可选:开启调试端口,方便后续连接 options.addArguments("--remote-debugging-port=9222"); WebDriver driver = new ChromeDriver(options); driver.get("https://google.com");
内容的提问来源于stack exchange,提问作者likenzy
相关产品推荐
相关产品推荐

