Adding extension to Selenium2(WebDriver) chrome driver

后端 未结 2 1295
感情败类
感情败类 2021-01-20 23:01

I\'m using the code below to start chrome using webdriver (selenium 2)

            Map mobileEmulation = new HashMap(         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-20 23:24

    Finally figured it out!

    Per the ChromeDriver capabilites page, you need to convert the .crx file into a base-64 encoded string. So the final answer will look something like this:

    ArrayList 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);
    

    Where Data.base64encoder() is my custom method for encoding. There are plenty of examples of how to do that based on the version of Java you're running. Basically send it the location, have it read in the binary, and return a string.

提交回复
热议问题