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:

1

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:

1
pip install selenium

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

1
2
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:

1
2
3
4
5
6
7
8
url = "http://localhost:55438/Default.aspx"

# Load wanted page
driver.get(url)

# 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:

1
2
3
4
5
6
7
# 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)))
else:
    print("Cookie does not exist!")

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

1
2
driver.find_element_by_id("cookie_value").send_keys("form submit")
driver.find_element_by_id("form_submit").click()

Alternatively, we can change current cookie:

1
2
3
4
5
6
7
8
9
10
11
12
13
cookie = driver.get_cookie("demo")
if cookie:
    cookie["value"] = base64.b64encode("set cookie")
else:
    cookie = { "domain" : "localhost",
               "expiry" : None,
               "httpOnly" : False,
               "name" : "demo",
               "path" : "/",
               "secure" : False,
               "value" : base64.b64encode("set cookie") }
driver.delete_cookie("demo")
driver.add_cookie(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.

– Alexander

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome content in your inbox, as soon as it is published!