繁体   English   中英

如何使用Java中的Selenium在具有自己创建的扩展名的Firefox中处理代理身份验证?

[英]How to handle proxy authentication in Firefox with self-created extension using Selenium in Java?

我遵循了使用PythonSelenium主题上提供的@Mike解决方案:输入/提供firefox的http代理密码 ,它非常有效地通过插件解决了Chrome上的代理身份验证问题。

现在,我在Firefox浏览器中面临着同样的问题:我使用Selenium编写Java语言,并且试图创建一个扩展名(将其打包到.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, I read somewhere that should work, but i'm not too sure

     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'

我还尝试在调试模式下手动在Firefox上添加扩展名,以便Firefox在没有签名的情况下也不会拒绝它,但它无法正常工作。 似乎它可以识别插件,但没有应用。

我对.xpi格式和我在某处读取的文件.rdf存入.xpi存有疑问,该文件应该提供有关插件安装的说明,但我不太了解。

感谢您的支持:)

编辑:我并没有被迫使用插件,我要获得的最终结果是使用不同的数据进行自动代理身份验证,然后让用户自由浏览。 我已经在寻找一个不太复杂的解决方案,但是找到了。

EDIT2:我解决了问题。这不是我的代码,除非您使用开发人员Firefox版本并将配置文件首选项xpinstall.signatures.required设置为false否则Firefox浏览器不再接受未签名的扩展。

(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,因为它之前没有初始化。 你有为你工作吗?

暂无
暂无

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

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