Skip to content

Instantly share code, notes, and snippets.

@gmmoreira
Created September 29, 2021 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmmoreira/f586529e5e1902b763906c8151e8519c to your computer and use it in GitHub Desktop.
Save gmmoreira/f586529e5e1902b763906c8151e8519c to your computer and use it in GitHub Desktop.
Attach Watir (and Selenium) to an already running browser (Chrome)
# I had to automate a few tasks in my browser with my current logged session, but by default
# Selenium always starts a new session.
# After some research, the only way I found to attach Selenium to my running browser session
# was through the debugger address option.
# Start chrome with the command line option: google-chrome --remote-debugging-port=8181
# Then start selenium webdriver with the debug address option (I could only find the option
# in latest selenium-webdriver gem (v4.0))
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'watir'
gem 'rexml'
# So far, only selenium-webdriver 4.0 has support for debugger_address option
gem 'selenium-webdriver', '~> 4.0.0.rc1'
end
require 'watir'
require 'rexml'
options = Selenium::WebDriver::Chrome::Options.new(debugger_address: '127.0.0.1:8181')
driver = Selenium::WebDriver.for(:chrome, options: options)
browser = Watir::Browser.new(driver)
# do whatever you need: browser.goto('https://foo.bar'); browser.div(id: 'id123')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment