A Petrov
A Petrov

Reputation: 484

How to get Selenium WebDriver to use Socks proxy?

I've got this code:

proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    "socksProxy": "192.200.208.5:80", #edited
    "socksUsername" :"username", #edited
    "socksPassword" : "password", #edited
    'noProxy':''})

driver = webdriver.Firefox(proxy=proxy)
driver.get('http://whatismyip.com')

When I execute this script it opens a Firefox browser, but it gets "stuck" while loading the page. If I use this code instead:

PROXY = "192.200.208.5:80:username:password"
proxy = Proxy({
    "httpProxy":PROXY,
    "ftpProxy":PROXY,
    "sslProxy":PROXY,
    'proxyType': ProxyType.MANUAL,
    'noProxy':''})

I get asked to fill in my proxy's username/password in a popup window. When I do type in the credentials it loads whatismyip.com normally and I can see that I am using the proxy.

I'd like that to happen automatically, but I am not sure why doesn't the code above work.

I am not sure if the proxy is a "socks" proxy, but it is the only one that has username/password, so I assume that I am in the right direction.

Upvotes: 2

Views: 4827

Answers (1)

mrtipale
mrtipale

Reputation: 979

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. I don't see creds support for SOCKS proxy.

Upvotes: 4

Related Questions