'How can I set socks5 proxy for selenium webdriver? Python

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.


Solution 1:[1]

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)

Solution 2:[2]

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)

Solution 3:[3]

    'proxy': {
        'http': 'socks5://user:[email protected]:8888',
        'https': 'socks5://user:[email protected]:8888',
        'no_proxy': 'localhost,127.0.0.1'
    }
}
driver = webdriver.Chrome(seleniumwire_options=options)```

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

Solution 4:[4]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tanmay Harsh
Solution 2 Dharman
Solution 3 Alex
Solution 4 tsh