问题描述
我需要使用 Selenium 来启动 Edge (Chromium) 浏览器。我可以毫无问题地做到这一点。
但是,我想在安装了解压扩展的情况下启动上述浏览器。我有扩展的路径。通过在 --load-extentsion=<path>
中添加一个名为 ChromeOptions
的参数,我可以在 Chrome 中执行此操作。但是,这不适用于 Edge Chromium。
并使用以下代码:
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();
}
浏览器启动正常,但我的扩展程序没有加载。
我需要能够加载解压的扩展程序,而不必将其发布到 Chrome 或 Edge 应用商店。
有人可以帮我吗?
谢谢 斯里拉姆
解决方法
你应该像这样添加扩展:
var outPutDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var extensionName = "3.1.3_0.crx";
var options = new EdgeOptions();
options.AddExtensionPath($@"{outPutDirectory}\{extensionName}");
或者您可以使用 AddExtension(string pathToExtension)