Skip to content

Instantly share code, notes, and snippets.

@alloy-d
Created January 5, 2015 22:15
Show Gist options
  • Save alloy-d/6d3b2d65b0e50874019f to your computer and use it in GitHub Desktop.
Save alloy-d/6d3b2d65b0e50874019f to your computer and use it in GitHub Desktop.
Finish sentences using Google autocomplete!
require 'capybara'
require 'forwardable'
class GoogleCompleter
extend Forwardable
attr_accessor :session
def_delegator :session, :evaluate_script, :js
def initialize
@session = Capybara::Session.new(:selenium)
@session.visit("https://www.google.com")
end
def complete(fragment)
session.fill_in "q", with: fragment
sleep 0.3
results = js <<-EOJS
(function () {
var els = document.querySelectorAll("[role='listbox'] [role='option'] b:last-child");
return Array.prototype.map.call(els, function (el) {
return el.innerHTML.replace(/^\s+/, "");
});
}())
EOJS
end
def quit
session.driver.browser.quit
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment