簡體   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