BrowserStack中Selenium3启动Edge InPrivate隐私模式的配置方案
Selenium 3.x 在BrowserStack启动Edge InPrivate隐私模式不生效解决
问题场景
在Selenium 3.141.59版本下对接BrowserStack云测平台时,Chrome浏览器通过addArguments("incognito")可以正常拉起无痕模式,但Edge浏览器无论怎么配置InPrivate相关capability,始终以普通模式启动。
最初的无效配置代码如下:
caps.setCapability("os_version", osVersion); caps.setCapability("os", os); caps.setCapability("resolution", "1920x1080"); caps.setCapability("browser", browser); caps.setCapability("browser_version", browserVersion); if(isPrivate && browser.equalsIgnoreCase("chrome")) { ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.addArguments("incognito"); caps.setCapability(ChromeOptions.CAPABILITY, chromeOptions); } else if(isPrivate && browser.equalsIgnoreCase("edge")) { EdgeOptions edgeOptions = new EdgeOptions(); edgeOptions.setCapability("InPrivate",true); caps.merge(DesiredCapabilities.edge().merge(edgeOptions)); }
- 版本约束:项目固定使用Selenium 3.141.59,暂无法升级Selenium大版本,对应pom依赖:
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency>
修复步骤
- 第一步:引入微软官方的Edge Selenium适配工具包,在pom.xml中添加如下依赖:
<dependency> <groupId>com.microsoft.edge</groupId> <artifactId>msedge-selenium-tools-java</artifactId> <version>3.141.1</version> </dependency>
- 第二步:替换EdgeOptions的导入路径,不要使用Selenium原生包下的EdgeOptions,改为导入微软工具包内的对应类,通过启动参数传入inprivate配置即可生效:
// 注意导入的是微软工具包下的EdgeOptions,不是org.openqa.selenium包下的 import com.microsoft.edge.seleniumtools.EdgeOptions; // Edge隐私模式配置 EdgeOptions edgeOptions = new EdgeOptions(); edgeOptions.addArguments("inprivate"); // 后续将edgeOptions合并到DesiredCapabilities中,传给BrowserStack驱动即可正常拉起InPrivate模式的Edge浏览器
内容的提问来源于stack exchange,提问作者user1826602
相关产品推荐
相关产品推荐

