Last active
March 22, 2020 21:31
-
-
Save sivajankan/1e5e283fa80b48db079fc425e7492672 to your computer and use it in GitHub Desktop.
Google Search With Chrome Gecko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'capybara' | |
require 'capybara/dsl' | |
require 'io/console' | |
require 'csv' | |
Capybara.run_server = false | |
#TODO: download selenium driver and put it PATH https://www.guru99.com/gecko-marionette-driver-selenium.html | |
Capybara.register_driver :selenium_firefox do |app| | |
Capybara::Selenium::Driver.new(app, | |
:browser => :firefox, | |
) | |
end | |
#TODO: Download appropriate geckodriver from https://github.com/mozilla/geckodriver/releases put it in the PATH | |
Capybara.register_driver :selenium_chrome do |app| | |
Capybara::Selenium::Driver.new(app, | |
:browser => :chrome, | |
desired_capabilities: { | |
"chromeOptions" => { args: ["--window-size=1024,768"]} | |
}) | |
end | |
#Capybara.current_driver = :selenium_firefox | |
Capybara.current_driver = :selenium_chrome | |
class GoogleSearch | |
include Capybara::DSL | |
# opens default browser and do google search by browsing google.com with search query | |
def search(query) | |
#puts "\n\nSearch for: #{query}" | |
q = query.gsub(" ", "+") + "+sponsored+projects" | |
visit("https://www.google.com/search?q=#{q}") | |
print "\n#{query}," | |
count = 0 | |
#no need to do more than 10 words of search | |
while (count < 10) | |
c = STDIN.getch | |
if c == "\r" #go back if return pressed | |
return | |
elsif c == "\t" #quit app if tab pressed | |
exit 0 | |
else | |
print `pbpaste` #print the clipboard content | |
end | |
count += 1 | |
print "," #for csv formatting | |
end | |
end | |
end | |
#array = ["Dartmouth College"] | |
gs = GoogleSearch.new | |
print "press space (paste), return (next) or tab (terminate) \r\n" | |
#array.each { |a| gs.search(a) } | |
#file contains only one column of college name (or search entry) | |
csv = CSV.read("files/colleges.csv") | |
csv.map {|a| gs.search(a[0])} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment