Skip to content

Instantly share code, notes, and snippets.

@turicas
Created March 22, 2025 16:03
Show Gist options
  • Save turicas/911cef28a9fa8c8872c6ff1c98a4abe7 to your computer and use it in GitHub Desktop.
Save turicas/911cef28a9fa8c8872c6ff1c98a4abe7 to your computer and use it in GitHub Desktop.
Configure SOCKS proxy in Splinter for both Firefox and Chrome (with and without selenium-wire)

Configuring a Socks Proxy in Splinter

I only found documentation regarding HTTP proxies on Splinter, but sometimes I just want to run ssh -D 1234 some-machine and get the SOCKS proxy on port 1234 to work with the Splinter browser. After many tests, I created these examples for Firefox and Chrome and all of them work.

For Web scraping tasks, sometimes having access to all the requests the browser made is a requirement, so I made examples for making this work with selenium-wire also. Since selenium-wire creates a proxy (so it can intercept requests between Selenium and the browser), the proxy configurations must be passed in a different way.

Note: I prefer to pass options instead of capabilities because it works for both Firefox and Chrome (only the Firefox splinter Webdriver has the capabilities parameter).

selenium
splinter
selenium-wire
blinker == 1.7.0
from splinter import Browser
from selenium.webdriver import Proxy
from selenium.webdriver.chrome.options import Options
SOCKS_HOST = "localhost"
SOCKS_PORT = 1234
options = Options()
proxy = Proxy(raw={"socksProxy": f"{SOCKS_HOST}:{SOCKS_PORT}", "socksVersion": 5})
options.set_capability("proxy", proxy.to_capabilities())
browser = Browser("chrome", headless=False, options=options)
browser.visit("https://browserleaks.com/ip")
screenshot_filename = browser.screenshot("/tmp/splinter-chrome-socks-")
print(f"Screenshot at: {screenshot_filename}")
browser.quit()
from seleniumwire.webdriver import Chrome as ChromeWireDriver
from splinter.browser import get_driver
from splinter.driver.webdriver import chrome as splinter_chrome
SOCKS_HOST = "localhost"
SOCKS_PORT = 1234
sw_options = {
"proxy": {
"http": f"socks5://{SOCKS_HOST}:{SOCKS_PORT}",
"https": f"socks5://{SOCKS_HOST}:{SOCKS_PORT}",
},
}
splinter_chrome.Chrome = ChromeWireDriver
browser = get_driver(
splinter_chrome.WebDriver,
headless=False,
seleniumwire_options=sw_options,
)
browser.visit("https://browserleaks.com/ip")
print("Requests found to browserleaks.com:")
for request in browser.driver.requests:
if request.url.startswith("https://browserleaks.com/"):
print(request.url)
screenshot_filename = browser.screenshot("/tmp/splinter-chrome-seleniumwire-socks-")
print(f"Screenshot at: {screenshot_filename}")
browser.quit()
from splinter import Browser
from selenium.webdriver import Proxy
from selenium.webdriver.firefox.options import Options
SOCKS_HOST = "localhost"
SOCKS_PORT = 1234
options = Options()
proxy = Proxy(raw={"socksProxy": f"{SOCKS_HOST}:{SOCKS_PORT}", "socksVersion": 5})
options.set_capability("proxy", proxy.to_capabilities())
browser = Browser("firefox", headless=False, options=options)
browser.visit("https://browserleaks.com/ip")
screenshot_filename = browser.screenshot("/tmp/splinter-firefox-socks-")
print(f"Screenshot at: {screenshot_filename}")
browser.quit()
from seleniumwire.webdriver import Firefox as FirefoxWireDriver
from splinter.browser import get_driver
from splinter.driver.webdriver import firefox as splinter_firefox
SOCKS_HOST = "localhost"
SOCKS_PORT = 1234
sw_options = {
"proxy": {
"http": f"socks5://{SOCKS_HOST}:{SOCKS_PORT}",
"https": f"socks5://{SOCKS_HOST}:{SOCKS_PORT}",
},
}
splinter_firefox.Firefox = FirefoxWireDriver
browser = get_driver(
splinter_firefox.WebDriver,
headless=False,
seleniumwire_options=sw_options,
)
browser.visit("https://browserleaks.com/ip")
print("Requests found to browserleaks.com:")
for request in browser.driver.requests:
if request.url.startswith("https://browserleaks.com/"):
print(request.url)
screenshot_filename = browser.screenshot("/tmp/splinter-firefox-seleniumwire-socks-")
print(f"Screenshot at: {screenshot_filename}")
browser.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment