Hi guys,

In this post, I will demonstrate that how you can make your Firefox/PhantomJS webdriver go via a socks4/5 proxy. In this scenario, we will assume that we want our webdriver to go via a locally set up TOR SOCKS proxy running on port 9050. The code for adding proxy setting in Firefox is:

Code:
def install_firefox_proxy(PROXY_HOST,PROXY_PORT):
    fp = webdriver.FirefoxProfile()
    fp.set_preference("network.proxy.type", 1)
    fp.set_preference("network.proxy.socks", PROXY_HOST)
    fp.set_preference("network.proxy.socks_port", int(PROXY_PORT))
    fp.update_preferences()
    return webdriver.Firefox(firefox_profile=fp)

# To create our webdriver with these settings, we can simply call this as
driver = install_firefox_proxy("127.0.0.1", 9050)

# Check if this works
driver.get("http://ip.telize.com").source_code

For adding the same functionality to a PhantomJS webdriver, we can use the following code:

Code:
# PhantomJS Driver
service_args = [
    '--proxy=127.0.0.1:9050', # You can change these parameters to your liking.
    '--proxy-type=socks5', # Use socks4/socks5 based on your usage.
    ]
driver = webdriver.PhantomJS(service_args=service_args)

# Check if this works
driver.get("http://ip.telize.com").source_code
Hope this helps. Cheers.

Regards,
c0dist