Java 向Selenium2(WebDriver)chrome驱动程序添加扩展

Java 向Selenium2(WebDriver)chrome驱动程序添加扩展,java,google-chrome,selenium,selenium-chromedriver,Java,Google Chrome,Selenium,Selenium Chromedriver,我正在使用下面的代码使用webdriver(selenium 2)启动chrome Map mobileEmulation=new HashMap(); mobileEmulation.put(“deviceName”、“BlackBerry PlayBook”); Map chromeOptions=new HashMap(); chromeOptions.put(“移动模拟”,移动模拟); DesiredCapabilities=DesiredCapabilities.chrome(); 能

我正在使用下面的代码使用webdriver(selenium 2)启动chrome

Map mobileEmulation=new HashMap();
mobileEmulation.put(“deviceName”、“BlackBerry PlayBook”);
Map chromeOptions=new HashMap();
chromeOptions.put(“移动模拟”,移动模拟);
DesiredCapabilities=DesiredCapabilities.chrome();
能力。设置能力(ChromeOptions.CAPABILITY,ChromeOptions);
驱动程序=新的色度驱动程序(功能);
如何在保留上述选项的同时加载Chrome扩展?

使用以下代码:-

ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);

希望它能帮助你:)

终于解决了

根据,您需要将.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);
arraylistext=newarraylist();
extensionLocation=extensionDir+sep+extensionName+“.crx”;
扩展名=新文件(扩展名位置);
if(extension.exists()&&!extension.isDirectory()){
ext.add(Data.base64Encoder(extensionLocation));
}
chromeOptions.put(“扩展名”,ext);

其中
Data.base64encoder()
是我的自定义编码方法。基于您正在运行的Java版本,有很多例子说明了如何做到这一点。基本上是将其发送到位置,以二进制形式读取,然后返回字符串。

您可以参考:-谢谢,但我也想使用移动仿真设置打开chrome。我也在寻找一种方法来实现这一点。但我发现的每个示例都只加载了一个扩展,而没有其他扩展。如何加载扩展并设置任何其他值、开关或模拟模式?
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);