This is a card in Dave's Virtual Box of Cards.

Using a SOCKS5 proxy with Selenium WebDriver with Ruby

Created: 2022-03-18

For my specific task, I needed to have Selenium WebDriver connect to a site with:

See ruby-selenium for my most basic Chrome + WebDriver + Ruby setup.

Here’s a snippet of my script establishing the proxy with ssh:

# Start SSH session.
#   -D create dynamic SOCKS (v5) proxy at specified port
#   -N don't execute any commands, just proxy
#   -f background process
#   -q quiet mode - hides some unhelpful warnings
ssh -D $proxy_port -N -f -q $remote_machine

The $proxy_port is 8888 and $remote_machine is an entry in my .ssh/config file.

Here’s a foo.rb WebDriver script connecting to a site only reachable through the above proxy:

require 'selenium-webdriver'

options = Selenium::WebDriver::Chrome::Options.new

# use the proxy!
options.add_argument '--proxy-server=socks5://localhost:8888'

# leave open when chromedriver quits
options.add_option(:detach, true)

driver = Selenium::WebDriver.for(:chrome, capabilities: options)

driver.get 'https://clown.website.fart/'

Holy heck was it ever hard to piece that together. Especially since much of the Selenium WebDriver info out there "in the wild" is for Python and I’m getting my feet back into the Ruby world.

I’m begging API documentation authors: please provide small, complete examples when possible! They’re worth their weight in gold.

Some useful external links: