Seting up proxies in selenium

Viewed 46

I need to set up ip:port:user:pass proxies in selenium to automatically refresh after 10 executes of script. How i can do this? I'm using Python selenium

var config = {
    mode: "fixed_servers",
    rules: {
        singleProxy: {
            scheme: "http",
            host: "X",
            port: parseInt(X)
        },
        bypassList: ["foobar.com"]
    }
};

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

function callbackFn(details) {
    return {
        authCredentials: {
            username: "X",
            password: "X"
        }
    };
}

1 Answers

Why not generating your `background.js file dinamically?


import zipfile


PROXY_HOSTS = ['proxy1', 'proxy2']
PROXY_PORT = 8080
PROXY_USER = ['user1', 'user2'] 
PROXY_PASS = ['password1', 'password2']

backgroundjs = """
var config = {
        mode: "fixed_servers",
        rules: {
          singleProxy: {
            scheme: "http",
            host: "{0}",
            port: parseInt({1})
          },
          bypassList: ["foobar.com"]
        }
      };

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

function callbackFn(details) {
    return {
        authCredentials: {
            username: "{2}",
            password: "{3}"
        }
    };
}
""".format(PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)

Then:

Create zip with your vars:

myproxies = zip(PROXY_HOSTS, PROXY_USER, PROXY_PASS)

You will get something like:

[('proxy1', 'user1', 'password1'), ('proxy2', 'user2', 'password2')]

Iterate and save:

with zipfile.ZipFile(pluginfile, 'w') as selzip:
    selzip.writestr("background.js", backgroundjs)
Related