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

时间:2019-04-04 18:36:42

标签: java firefox-addon selenium-firefoxdriver

我遵循了本主题Selenium using Python: enter/provide http proxy password for firefox上提供的@Mike解决方案 并且非常适合通过插件解决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 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浏览器将不再接受未签名的扩展名。

1 个答案:

答案 0 :(得分:0)

  

ld

我已通过更改manifest.json中指定的附件名称来解决此问题,因为Windows文件不允许(和|。

但是我似乎还有另一个错误: Firefox无法安装该插件,因为它说它已损坏。 在Firefox中调试时,出现问题

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

因为它在firefox上获得了参考错误,因为它之前没有初始化。 你为它工作了吗?

相关问题