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

风流意气都作罢 提交于 2020-05-14 15:56:53

问题


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).


回答1:


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);



回答2:


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.



来源:https://stackoverflow.com/questions/54609394/how-to-change-proxy-on-my-webdriver-multiple-times-on-a-single-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!