How can I set socks5 proxy for selenium webdriver? Python

Viewed 15922

I really can’t to set socks5 proxy(http too...) for my chrome webdriver in selenium for python. I tried many different ways... But I think I do something bad.

Example 1:

self.options.add_argument('--proxy-server=http://'+proxy)

Example 2:

webdriver.DesiredCapabilities.CHROME['proxy'] = {
        "socksProxy": proxy,
        "ftpProxy": proxy,
        "sslProxy": proxy,
        "noProxy": None,
        "proxyType": "MANUAL",
        "class": "org.openqa.selenium.Proxy",
        "autodetect": False
    }

Please describe fully the working example of setting up socks5 proxy on Selenium for Python and Chrome webdriver, with an example of proxy string formats (maybe i am doing something mistakes here ...).

PS Two problems which I get:

  1. Just staying old IP address.
  2. No internet connection in chrome web driver.
5 Answers

Chrome do not allow proxy with auth. I am not shure but after read so many informations I think so.... Only one way is working for me - to use proxy socks5 without auth by login and password.

 options = webdriver.ChromeOptions()
 proxy = '12.12.421.125:1949'   
 options.add_argument('--proxy-server=socks5://' + proxy)
 driver = webdriver.Chrome(options=options)

For FireFox's geckodriver if you just want to set socks5 host / socks5 proxy :-

form selenium import webdriver

profile = webdriver.FirefoxProfile()

# Socks5 Host SetUp:-
myProxy = "198.199.101.152:8388"
ip, port = myProxy.split(':')
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', ip)
profile.set_preference('network.proxy.socks_port', int(port))

driver = webdriver.Firefox(firefox_profile=profile)

Here is the code I used to connect to a Socks5 server with username/password auth.

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

capabilities = dict(DesiredCapabilities.CHROME)
capabilities['proxy'] = {
    'proxyType': 'MANUAL',
    'socksProxy': '<Your_IP>:<Your_Port>',
    'socksVersion': 5,
    'ftpProxy': '<Your_IP>:<Your_Port>',
    'noProxy': 'localhost,127.0.0.1',
    'class': "org.openqa.selenium.Proxy",
    'autodetect': False
}

capabilities['proxy']['socksUsername'] = '<username>'
capabilities['proxy']['socksPassword'] = '<password>'

driver = Chrome(ChromeDriverManager().install(), desired_capabilities=capabilities)
    'proxy': {
        'http': 'socks5://user:pass@192.168.10.100:8888',
        'https': 'socks5://user:pass@192.168.10.100:8888',
        'no_proxy': 'localhost,127.0.0.1'
    }
}
driver = webdriver.Chrome(seleniumwire_options=options)```

source - https://github.com/wkeeling/selenium-wire#socks

For Firefox's geckodriver , based on the answer by Tanmay Harsh, I have got this so far:

from selenium.webdriver import Firefox, FirefoxOptions

proxy = ('proxy-server.local', 1080)

options = FirefoxOptions()
options.set_preference('network.proxy.type', 1)
options.set_preference('network.proxy.socks', proxy[0])
options.set_preference('network.proxy.socks_port', proxy[1])
options.set_preference('network.proxy.socks_remote_dns', True)
driver = Firefox(options=options)
Related