Automating Browsers with Selenium

Automating browser actions from an external script is easier than you think.

The answer:  Selenium.

Selenium is a browser automation tool suite which is compatible with several popular browsers, such as Firefox, Chrome, Internet Explorer and Opera. It is primarily written in Java, but it also has extensions for Python and Ruby.

Building on my Python and Javascript experience,  my experience was mostly painless for Firefox and Internet Explorer.  Getting the extensions is fairly easy, and source code was straight forward.  I really like how I can grab html objects by id OR class or xml.  Here is a very simple example of using the Selenium python extension to 1) open Firefox, 2) navigate to google.com, 3) enter text into the search box, then 4) hit enter.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()
browser.get('http://google.com')
assert 'Google' in browser.title
searchBoxes = browser.find_elements_by_id('gbqfq')
searchBoxes[0].send_keys('hello' + Keys.RETURN)

Leave a Reply

Your email address will not be published. Required fields are marked *