Simple Web Task Automator using Selenium WebDriver (Ruby Gem)

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.

Setup 

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

$PATH

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[2]/div[3]/center/input[1]')
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.

Issues

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.

Folder Action to Copy Dropbox Screenshots to a Different Directory using the built-in OSX Automator

For mac users who take a lot of Screenshots and also happen to have Dropbox, you may have noticed that the screenshots are automatically saved to the Dropbox/Screenshots subdirectory. Annoyingly, there is no way to change this destination within your dropbox preferences. My solution Here is a guide to setup the OS X Automator to take care of this for you, well, automatically.

The OSX Automator is a macro builder that comes standard with Apple computers. Macros are automated functions that can take care of many mindless repetitive tasks after being set up. I just found out about it recently when I came across this Dropbox issue and I want to share how I set it up.

Right click on the folder and select Services > Folder Actions Setup…

 

A modal box titled “Folder Actions Setup” should appear. Select “move – source to dest files.scpt” then click Attach.

The name of the folder should appear on the Folders With Actions list. Make sure it is checked. Now we are capable of adding macros to the folder using the Automator.

Open up Automator from Spotlight Search or the Applications folder.

Select New Workflow then click on Files & Folders from the left navigation menu

You should see an empty workspace with the message “Drag actions or files here to build your workflow.” The first thing to do is get the specified finder items, so find Get Specified Finder Items from the searchable action list and drag it over.

Click the Add button to find the folder from earlier.

Next, we want to duplicate the file so we don’t lose the original.

Drag that below the Get Specified… item in your workflow. Lastly, we want to move the duplicated file to the new directory. You can do this by using the Move Finder Items action.

Drag this below the Duplicate Finder Items action.

Select where you are moving the files from the dropdown “To” selector.

Your total workflow should look something like this:

Go ahead and click the Run button at the top and test it out by moving taking a few screenshots.

Hope this helped!