简体   繁体   中英

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

I followed @Mike solution provided at this topic Selenium using Python: enter/provide http proxy password for firefox and it worked great to solve the proxy authentication issue on Chrome through the plugin.

Now I'm facing the same problem with Firefox browser: i'm in Java, using Selenium and i'm trying to create an extension (packing it into .xpi), that would make the same job.

So what i'm doing right now in my code is the following:

    - Taking from pojo's file a pre-written manifest.json and background.js and modifying the script inserting my proxy datas. This is how they appear:

     **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'] ); 

    - Creating a .zip file but naming it as .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(); } 

    - Set a Firefox profile, adding the extension and run the 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); 

When I run all this, the following error code appear at driver declaration line:

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'

I also tried to add the extension on Firefox manually in debugging mode so that Firefox doesn't refuse it also if it doesn't have a signature, but it didn't work. It seem that it recognize the plugin but it didn't apply it.

I have doubts about the .xpi format and a file .rdf that I read somewhere should be into the .xpi and that should give instructions about the plugin installation, but i didn't understand much.

Thanks for any support :)

EDIT: I'm not forced to use the plugin by the way, the final result that i want to obtain is an auto proxy authentication made dinamically with different datas and letting the user navigate freely after that. I already searched for a less complex solution but found any.

EDIT2: I solved the problem.. It was not my code, Firefox browser does not accept anymore not signed extensions unless you use a developer Firefox version and set the profile preference xpinstall.signatures.required to false .

(The syntax of the file name, directory or volume is incorrect)

I've fixed that by changing the name of the Add-On specified in the manifest.json because ( and | are not allowed for Windows Files.

But I appear to have another Error: Firefox can´t install the Plugin cause it says it´s corrupted. When I debug it in Firefox, it has Problems with

firefox.proxy.settings.set({value: config, scope: "regular"}, function() {});

Because it gets an Reference-Error on firefox, because it was not initialized before. Have you got it working for you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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