简体   繁体   English

Selenium WebDriver Java 启动带有解压扩展的 Edge Chromium

[英]Selenium WebDriver Java launch Edge Chromium with an unpacked extension

I need to use Selenium to launch the Edge (Chromium) browser.我需要使用 Selenium 来启动 Edge (Chromium) 浏览器。 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 .我可以通过在我的ChromeOptions中添加一个名为--load-extentsion=<path>的参数在 Chrome 中执行此操作。 However, the same doesn't work for Edge Chromium.但是,这对 Edge Chromium 不起作用。

Reading the docs for the MsEdgeDriver, I found this:阅读 MsEdgeDriver 的文档,我发现了这一点: 官方网站中的文档

So, I tried to package the extension using this button shown in the image below:因此,我尝试使用下图所示的此按钮对 package 扩展名: 从 Edge 扩展页面打包扩展

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.我需要能够加载解压缩的扩展程序,而不必在 Chrome 或 Edge 应用商店上发布它。

Can anyone help me?谁能帮我?

Thanks Sriram谢谢斯里拉姆

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)或者,您可以使用AddExtension(string pathToExtension)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM