Authenticate via Selenium/PhantomJS and pass cookies to Net::HTTP for subsequent requests

In this code snippet, I’ll share some code that allows you to authenticate to a website using Selenium/PhantomJS, and then pass the generated cookies to Net::HTTP for subsequent requests, for example to download a file.

#!/usr/bin/env ruby

# require selenium gem
require 'selenium-webdriver'

# define auth credentials
auth_username = 'YOURUSER'
auth_password = 'YOURPASSWORD'

# setup selenium with phantomjs driver
browser_user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36"
capabilities = Selenium::WebDriver::Remote::Capabilities.phantomjs("phantomjs.page.settings.userAgent" => browser_user_agent)
driver = Selenium::WebDriver.for :phantomjs, :desired_capabilities => capabilities

#
# Example code to authenticate
#

# log in
driver.navigate.to 'http://www.example'

# username / email
element = driver.find_element :css, "form#new_user #user_email"
element.clear
element.send_keys auth_username

# password
element = driver.find_element :css, "form#new_user #user_password"
element.clear
element.send_keys auth_password

# submit button
element = driver.find_element :css, "form#new_user input[name='commit']"
element.click

#
# Begin Cookie transition code
#

# collect current cookies
cookies = driver.manage.all_cookies.map {|c| "#{c[:name]}=#{c[:value]}"}.join '; '

# create http GET request
http = Net::HTTP.new('www.example.com', 443)
http.use_ssl = true
http_path = "/some_download_path"

response = http.get(
  http_path, {
    'Cookie' => cookies,
    'User-Agent' => browser_user_agent,
    'Referer' => 'http://www.example.com',
  }
)

# example.. write out file
File.open(file_absolute_path, 'w') { |file| file.write(response.body) }

driver.quit

Updated: