Skip to content

Instantly share code, notes, and snippets.

@gmmoreira
Created September 29, 2021 20:48
Show Gist options
  • Save gmmoreira/f586529e5e1902b763906c8151e8519c to your computer and use it in GitHub Desktop.
Save gmmoreira/f586529e5e1902b763906c8151e8519c to your computer and use it in GitHub Desktop.

Revisions

  1. gmmoreira created this gist Sep 29, 2021.
    26 changes: 26 additions & 0 deletions script.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    # 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')