我正在使用下面的代码使用webdriver(硒2)启动chrome
Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "BlackBerry PlayBook");
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
driver = new ChromeDriver(capabilities);
如何在保留上述选项的同时加载Chrome扩展程序?
解决方法:
终于想通了!
对于the ChromeDriver capabilites page,您需要将.crx文件转换为base-64编码的字符串.因此,最终答案将如下所示:
ArrayList<String> ext = new ArrayList<>();
extensionLocation = extensionDir + sep + extensionName + ".crx";
extension = new File(extensionLocation);
if (extension.exists() && !extension.isDirectory()) {
ext.add(Data.base64Encoder(extensionLocation));
}
chromeOptions.put("extensions", ext);
其中Data.base64encoder()是我的自定义编码方法.有许多示例基于您所运行的Java版本来执行此操作.基本上将其发送到位置,使其读入二进制文件,然后返回一个字符串.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。