M
M
MoHaLLlKa2017-02-13 00:36:57
Python
MoHaLLlKa, 2017-02-13 00:36:57

How to change proxy while working with selenium webdriver chrome?

When working with selenium, I want to change the proxy on the fly for one webdriver without recreating it.
How is it possible to implement this? I can set a proxy before creating a webdriver, but the problem is that:
1) when the webdriver has already been created and I feel like changing the proxy, how can I do this?
2) how to use proxies in selenium, whose authentication goes through username:password?
(note: on the screen, installing proxies before creating webdriver'a)
3f5b7e5929b4493aaf22f18ec3bc2f59.PNG

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeniy _, 2017-02-14
@MoHaLLlKa

To the second question:
Create a background.js file, substituting your values:

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

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

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

chrome.webRequest.onAuthRequired.addListener(
            callbackFn,
            {urls: ["<all_urls>"]},
            ['blocking']
);

Then create manifest.json:
{
    "version": "1.0.0",
    "manifest_version": 2,
    "name": "Chrome Proxy",
    "permissions": [
        "proxy",
        "tabs",
        "unlimitedStorage",
        "storage",
        "<all_urls>",
        "webRequest",
        "webRequestBlocking"
    ],
    "background": {
        "scripts": ["background.js"]
    },
    "minimum_chrome_version":"22.0.0"
}

pack both files in zip, ie proxy_auth.zip
Then load all this into the driver:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_extension("proxy_auth.zip")

driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=chrome_options)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question