Tutorial to minimally build an automator that can perform the following actions on an arbitrary website through your chrome browser:
- Find elements.
- Click buttons.
- Enter data into forms.
To accomplish this I used the Selenium Webdriver gem in ruby.
Download the latest chromedriver. If you are on a modern version of mac, use macos64 link. Put the downloaded file into your ruby env $PATH. To find that location in the terminal do
and look for a path that looks like /Users/name/.rbenv/shims and open that folder.
Move chromedriver into that directory.
Now open a directory and invoke
gem install selenium-webdriver
Browsing Through Pry
Open up pry and load in the webdriver
pry> require 'selenium-webdriver' #=> true
If that does not return ‘true’ there may be an issue with your selenium installation. For the rest of us, go ahead and load the webdriver into a variable and go to the website of interest.
pry> driver = Selenium::WebDriver.for :chrome pry> driver.navigate.to "https://google.com"
You should see a browser with the message “Chrome is being controlled by automated test software” as you navigate. This will be helpful for selecting the elements we are interested in. Open up the chrome inspect (cmd + option + i) and click on the selector tool (cmd + shift + c). For this tutorial, I will select the Google Search bar.
Right click on the element from the inspector and select “Copy XPath”
Now back in pry, set a new variable as our element of interest using the xpath key:
pry> element = driver.find_element(xpath: '//*[@id="q"]')
We can now set this using the send_keys method on our element.
pry> element.send_keys "puppies"
Now find the ‘Google Search’ button using the same method as before (inspector), get the xpath, and click on it.
pry> button = driver.find_element(xpath: '//*[@id="tsf"]/div/div/center/input') pry> button.click
If everything worked, puppies should appear! If you want to automate a similar task on a regular schedule, create a ruby file executable and set it to automatically run using cron jobs.
My chromedriver would occasionally remove itself from the ruby shims folder. To remedy this, I created a bash script that would copy chromedriver into this folder.
Ensure your chromedriver file is in a directory that is not in your $PATH and do the following:
Open a bash script file (foo.sh)
#!/bin/bash cp chromedriver '/Users/path/to/.rbenv/shims'
Ideally I would figure out the root of this error, but this hacky solution worked for me.