Web Browser Automation using Selenium

Recently, I was participating in an online challenge that included “hacking” to a website that was especially created for the challenge. After figuring out what to do at the beginning, I came to the conclusion that I need to send a particular html form multiple times (over 30) while changing cookie value between the form submits.

I don’t like doing repeatable thing manually so I started looking for a way how to automate it and found a nice automation tool called “Selenium“.

In this post, I want to do a short demonstration of how Selenium can be used for the given task using Python.

For the demonstration, I wrote a simple webpage that I’ve ran using Visual Studio and looks like this:


The webpage contains a single form with input box with id “cookie_value” and a submit button with id “form_submit”. On page load, the current cookie value is loaded to an element with id “current_cookie”. The cookie name is “demo” and it’s value is encoded using base 64 string.

Selenium python libraries can be easily installed using pip:

pip install selenium

Assuming all the pre-requirements are installed, we can start playing with Selenium:

from selenium import webdriver
driver = webdriver.Chrome()

driver is the instance of the browser object we will use for the automation. There are many supported browsers and you can choose whichever you like more.

Now, we can load the page and get the value of “current_cookie” from the display:

url = "http://localhost:55438/Default.aspx"

# Load wanted page

# Show cookie value as it shown in the webpage
webpage_value = driver.find_element_by_id("current_cookie").text
print("Current value (from webpage text): %s" % webpage_value)

We can also use the cookie object itself and read it’s value:

# Get actual cookie and show it's value
cookie = driver.get_cookie("demo")
if cookie:
    value = cookie["value"]
    print("Current cookie value is: %s (decoded: %s)" % (value, base64.b64decode(value)))
    print("Cookie does not exist!")

We can simulate a form submit by “typing” some text and clicking on the submit button:

driver.find_element_by_id("cookie_value").send_keys("form submit")

Alternatively, we can change current cookie:

cookie = driver.get_cookie("demo")
if cookie:
    cookie["value"] = base64.b64encode("set cookie")
    cookie = { "domain" : "localhost",
               "expiry" : None,
               "httpOnly" : False,
               "name" : "demo",
               "path" : "/",
               "secure" : False,
               "value" : base64.b64encode("set cookie") }

In conclusion, the Selenium library is a powerful tool with a lot of capabilities for web automation and it can be used for a various use cases.

The full demo can be found in the following link: Open GitHub

Output example:

==== Demo 1: Read initial web-site values ====
Current value (from webpage text): Cookie does not exist!
Cookie does not exist!
==== Demo 2: Automate web-page form filling ====
Current value (from webpage text): form submit
Current cookie value is: Zm9ybSBzdWJtaXQ= (decoded: form submit)
==== Demo 3: Update cookie ====
Current value (from webpage text): set cookie
Current cookie value is: c2V0IGNvb2tpZQ== (decoded: set cookie)

Feel free to play with the provided source code, it’s yours.



2 thoughts on “Web Browser Automation using Selenium

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s