我遵循了本主题提供的@Mike 解决方案 Selenium using Python: enter/provide http proxy password for firefox 通过插件解决 Chrome 上的代理身份验证问题效果很好。
现在我在使用 Firefox 浏览器时遇到了同样的问题:我在 Java 中,使用 Selenium 并且我正在尝试创建一个扩展(将其打包到 .xpi 中),这将完成同样的工作。
所以我现在在我的代码中所做的如下:
- - 从 pojo 的文件中获取一个预先编写的 manifest.json 和 background.js 并修改插入我的代理数据的脚本。它们是这样出现的:
**manifest.json
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Firefox Proxy | (Proxy Connector)",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
}
}
**background.js
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "https",
host: "%proxy_host",
port: parseInt("%proxy_port")
},
bypassList: []
}
};
firefox.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "%username",
password: "%password"
}
};
}
firefox.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
- 创建一个 .zip 文件但将其命名为 .xpi 我在某处读到应该有效,但我不太确定
try {
FileOutputStream fos = new FileOutputStream("./src/temp/plugin.xpi");
ZipOutputStream zipOS = new ZipOutputStream(fos);
writeToZipFile(manifestTemp.toString(), zipOS);
writeToZipFile(scriptModified.toString(), zipOS);
zipOS.close();
fos.close();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
private void writeToZipFile(String path, ZipOutputStream zipOS) throws IOException {
File aFile = new File(path);
FileInputStream fis = new FileInputStream(aFile);
ZipEntry zipEntry = new ZipEntry(aFile.getName());
zipOS.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOS.write(bytes, 0, length);
}
zipOS.closeEntry();
fis.close();
}
- 设置 Firefox 配置文件,添加扩展并运行 webdriver:
File extension = new File("./src/temp/plugin.xpi");
System.setProperty("webdriver.gecko.driver", "./src/WebDriver/geckodriver.exe");
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(extension);
profile.setPreference("extensions.firebug.onByDefault", true);
profile.setPreference("xpinstall.signatures.required", false);
FirefoxOptions options = new FirefoxOptions();
options.setBinary(new FirefoxBinary()); //don't know if this is correct
options.setProfile(profile);
this.driver = new FirefoxDriver(options);
当我运行所有这些时,以下错误代码出现在驱动程序声明行:
Exception in thread "AWT-EventQueue-0" org.openqa.selenium.firefox.UnableToCreateProfileException: java.io.FileNotFoundException: ...%user%\AppData\Local\Temp\anonymous1577938325377124354webdriver-profile\extensions\FirefoxProxy|(ProxyConnector)@1.0.0.xpi (The syntax of the file name, directory or volume is incorrect)
Build info: version: '3.141.59'
//geckodriver version '0.24.0'
我还尝试在 Debug模式下手动在 Firefox 上添加扩展,这样 Firefox 在没有签名的情况下也不会拒绝它,但它没有用。它似乎识别了该插件,但没有应用它。
我对 .xpi 格式有疑问,我在某处读到的一个文件 .rdf 应该是 .xpi 并且应该提供有关插件安装的说明,但我不太了解。
感谢您的支持:)
编辑: 顺便说一句,我并没有被迫使用该插件,我想要获得的最终结果是使用不同数据动态生成的自动代理身份验证,然后让用户自由导航。我已经搜索了一个不太复杂的解决方案,但找到了。
编辑 2:
我解决了这个问题。这不是我的代码,Firefox 浏览器不再接受未签名的扩展,除非您使用开发人员 Firefox 版本并将配置文件首选项 xpinstall.signatures.required
设置为 false
。
最佳答案
(The syntax of the file name, directory or volume is incorrect)
我已通过更改 manifest.json 中指定的附加组件的名称来解决此问题,因为 ( 和 | 不允许用于 Windows 文件。
但我似乎还有另一个错误: Firefox 无法安装插件,因为它说它已损坏。 当我在 Firefox 中调试它时,它有问题
firefox.proxy.settings.set({value: config, scope: "regular"}, function() {});
因为它在 firefox 上得到一个 Reference-Error,因为它之前没有被初始化。 你让它为你工作了吗?
关于java - 如何在 Java 中使用 Selenium 处理带有自创建扩展的 Firefox 中的代理身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55522874/