Sriram Sridharan
Sriram Sridharan

Reputation: 740

Selenium WebDriver Java launch Edge Chromium with an unpacked extension

I need to use Selenium to launch the Edge (Chromium) browser. I can do this without any problems.

However, I want to launch the said browser with an unpacked extension installed. I have the path for the extension. I'm able to do this in Chrome by adding an argument called --load-extentsion=<path> in my ChromeOptions. However, the same doesn't work for Edge Chromium.

Reading the docs for the MsEdgeDriver, I found this: Documentation in the official site

So, I tried to package the extension using this button shown in the image below: Packaging the extension from the Edge extensions page

And used the code below:

EdgeOptions edgeOptions = new EdgeOptions();
File extension = new File(extensionPath + File.separator + "my-extension.crx");
byte[] fileContent;
WebDriver webDriver;
try {
    fileContent = Files.readAllBytes(extension.toPath());
    edgeOptions.setCapability("extensions",Base64.getEncoder().encodeToString(fileContent));
    webDriver = new EdgeDriver(edgeOptions);
} catch (IOException e) {
    e.printStackTrace();
}

THe browser launches fine, but my extension is not loaded.

I need to be able to load an unpacked extension, without having to publish it on Chrome or Edge app stores.

Can anyone help me?

Thanks Sriram

Upvotes: 1

Views: 760

Answers (1)

Nikolay Advolodkin
Nikolay Advolodkin

Reputation: 2230

You should add the extension like this:

    var outPutDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    var extensionName = "3.1.3_0.crx";

    var options = new EdgeOptions();
    options.AddExtensionPath($@"{outPutDirectory}\{extensionName}");

Alternatively you can use the AddExtension(string pathToExtension)

Upvotes: 0

Related Questions