How to change proxy on my webdriver multiple times on a single session?

I was able to solve this issue by setting up the preferences through JS on aboutLconfig and then used execute_script in selenium to deploy the js through python:

regions = {
'US': '', #USA is the default server
'Australia': #json response through the api,
'Canada': #json response through the api,
'France': #json response through the api,
'Germany': #json response through the api,
'UK': #json request response the api
}   
    for region in regions:
        driver.get("about:config")
        time.sleep(3)
        driver.find_element_by_css_selector("window#config deck#configDeck vbox#warningScreen vbox#warningBox.container vbox.description hbox.button-container button#warningButton.primary").click()
        time.sleep(3)
        driver.execute_script('var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch); prefs.setIntPref("network.proxy.type", 1); prefs.setCharPref("network.proxy.socks", "' + regions[region] + '"); prefs.setIntPref("network.proxy.socks_port", 9998);')
        time.sleep(3)
        driver.get('https://www.whatsmyip.com/')
        time.sleep(10)

With the script im executing i am changing service value of the socks host and socks host with the region and the port respectively.

It essentially is the same as setting up a profile through selenium but this way you do it while the bot is running. You could also change user agent and pretty much anything you'd like this way.


According to this topic, here is your solution :

Solution link : Python Selenium Webdriver - Changing proxy settings on the fly

var setupScript=`var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);

prefs.setIntPref("network.proxy.type", 1);
prefs.setCharPref("network.proxy.http", "${proxyUsed.host}");
prefs.setIntPref("network.proxy.http_port", "${proxyUsed.port}");
prefs.setCharPref("network.proxy.ssl", "${proxyUsed.host}");
prefs.setIntPref("network.proxy.ssl_port", "${proxyUsed.port}");
prefs.setCharPref("network.proxy.ftp", "${proxyUsed.host}");
prefs.setIntPref("network.proxy.ftp_port", "${proxyUsed.port}");
                  `;    

//running script below  
driver.executeScript(setupScript);

//sleep for 1 sec
driver.sleep(1000);