Set proxy.socks.port selenium

rowasc picture rowasc · Sep 14, 2012 · Viewed 11.4k times · Source

I am used to setting http port like this:

profile.set_preference("network.proxy.http_port", "PORTNUMBER")

and that works . But now I need to connect with socks proxy and set the port, which it's not working

profile.set_preference("network.proxy.socks_port", "PORTNUMBER")

I couldn't find a reference in the docs and that's why I am asking here. Any ideas ? Is there a better way to do it ?

Thanks

Answer

mrtipale picture mrtipale · Sep 8, 2015

In your case, I think, you should use port as int instead of string. See below details

Lets understand first, how FF (or webdriver you use with Selenium) is setting SOCKS proxy.

For Firefox, do about:config in URL box.

network.proxy.socks;10.10.10.1
network.proxy.socks_port;8999
network.proxy.socks_remote_dns;true
network.proxy.socks_version;5

You can see same in prefs.js in FF profile director as below:

user_pref("network.proxy.socks", "10.10.10.1");
user_pref("network.proxy.socks_port", 8999);
user_pref("network.proxy.type", 1);

Note that, network.proxy.socks is string and it should be set as string only. Same goes for network.proxy.socks_port has to be int.

While setting it using selenium python module :

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.proxy import *
import time

# for fresh FF profile
#profile = webdriver.FirefoxProfile() 
profile_path="/path/to/custom/profile/"
profile = webdriver.FirefoxProfile(profile_path)
# set FF preference to socks proxy
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.socks", "10.10.10.1")
profile.set_preference("network.proxy.socks_port", 8999)
profile.set_preference("network.proxy.socks_version", 5)
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)

driver.get("http://whatismyip.com")
print driver.page_source
# sleep if want to show in gui mode. we do print it in cmd
time.sleep(25)
driver.close()
driver.quit()

Pls check if given preference is supported and present in FF about:config list.