Using a SOCKS5 proxy with Selenium WebDriver with Ruby
For my specific task, I needed to have Selenium WebDriver connect to a site with:
-
Chrome
-
Ruby
-
A SOCKS5 proxy established via SSH
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:
-
https://www.rubydoc.info/gems/selenium-webdriver/Selenium/WebDriver/Options Ruby WebDriver API docs - Options
-
https://www.rubydoc.info/gems/selenium-webdriver/Selenium/WebDriver/Chrome/Options specifically, chrome options
-
https://www.browserstack.com/guide/set-proxy-in-selenium (python example)