August 19, 2014

Automating menial Browser activity using Selenium

Today I stumbled across a marvelous Framework called Selenium which does browser automation. It lets you programmatically control a web browser session which can be used for automating tests or controlling browser to perform menial works.

Here's a small snippet that I put together for myself just to try out selenium's python bindings. Yes, python is the language of my choice currently! :) Selenium binds of various languages are available on their official website at :

What this script does is open a third party website:, log me in using my username, password and then send a message to any number of my choice. All of this is done programmatically! So all I've to do is fire up a console and enter my username, password, a mobile number and a message and selenium will handle the rest. Of course, this is just an example and one can use this to perform a variety of tasks, boring or otherwise.

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

def send(username, password, mobile, message):
  driver = webdriver.Firefox()



  frame = driver.find_element_by_id("frame")



You can install selenium python binding using:

sudo easy_install selenium

Since I hacked this together in hurry, it doesn't include any comments but the code should be self explanatory and easy to follow because all it does is find a web element (like an input box or a button) and send it a sequence of keys ( string value of action keys like RETURN ).

Automating tasks on a third party website requires a bit of inspection of web elements and javascripts, even stylesheet at times. This can be done using built-in web inspectors available with most of the modern browsers like Chrome, Firefox. As of now Firebug is the inspector of my choice.

Keep automating!

No comments:

Post a Comment