Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am working on a bot. I want the bot to change the proxy of the webdriver every 50 searches. I have an API that requests the proxy and the socket, i store those variables and so far i have been using firefox profiles to set it up but that doesnt work very well.

So given the fact that i already have a viable source of proxies and ports, can you tell me any way i can change the proxy without crashing the webdriver and doing it on a single session?

Previous attempts:

I tried setting up a firefox profile this way:

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:
        fp = webdriver.FirefoxProfile()
        if(regions[region] != ''):
            fp.set_preference("network.proxy.type", 1)
            fp.set_preference("network.proxy.socks", regions[region])
            fp.set_preference("network.proxy.socks_port", port)

This caused me some problems and i had to start a new session each time i wanted to swap a proxy. So i tried to change the proxy through the Firefox options (options - general - connection settings) but turns out the popup that appears on the screen after clicking the connection settings button is not accessible through selenium or javascript (xul file).

like image 266
SuperSimplePimpleDimple Avatar asked Feb 09 '19 18:02

SuperSimplePimpleDimple


2 Answers

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);
like image 192
slckayhn Avatar answered Nov 07 '22 02:11

slckayhn


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.

like image 23
SuperSimplePimpleDimple Avatar answered Nov 07 '22 03:11

SuperSimplePimpleDimple