• Home
  • Testimonials
  • Blog
  • Contact Us

At a Glance of a Key

Crafting Dreams into Ventures, Code into Excellence: Your Journey to Success

  • Home
  • Testimonials
  • Blog
  • Contact Us

Web Browser Automation using Selenium

2017-05-07 Development 2 Comments 2 minute read

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 a notification when new posts are published!

We don’t spam!

Check your inbox or spam folder to confirm your subscription.

Python

Building simple testing framework in Python

2 thoughts on “Web Browser Automation using Selenium”
  1. archer920gmailcom
    2017-05-08 at 11:56 PM

    We used Selenium for our Java site. I’m glad to hear it’s for Python also!!!

    Reply
    • alexandersirotin
      2017-05-09 at 10:46 AM

      Yes, there are many supported languages that can be used with this framework, inc. Java, C#, Python and more.

      Reply
Leave a Reply Cancel reply

About Me

Principal Software Engineer and an industry leader with startup and FAANG experience. I specialize in distributed systems, storage, data protection services and payment processors.

Beyond technical expertise, I am passionate about supporting fellow engineers in their careers. Through approachable blogs and hands-on guidance, I help navigate the ever-evolving landscape of technology, empowering individuals to thrive in their professional journeys.

Open LinkedIn

Recent Posts

  • Building a Delayed Message System with Redis and FastAPI
  • Go Concurrency, Practical Example
  • Using GORM – Part 3: Models and Idempotency
  • Using GORM – Part 2: Transactions and Save Points
  • Using GORM – Part 1: Introduction

Archives

  • January 2025
  • December 2024
  • March 2023
  • February 2023
  • September 2022
  • July 2022
  • July 2021
  • June 2021
  • February 2021
  • April 2018
  • March 2018
  • January 2018
  • July 2017
  • June 2017
  • May 2017

Categories

  • AWS
  • Career Growth
  • Cyber Security
  • Debugging
  • Development
  • Storage
  • Tips & Tricks

Tags

API AWS Azure Bash Brainfuck C++ Challenge Cloud Cloud Bursting Concurrency Database DevOps Disassembly DLL Documentation DynamoDB Go Golang Guice Java Jenkins Mossad NoSQL OOP Performance Programming Python Redis Security Serverless Singleton Streams Testing Unit Tests WebService

All Rights Reserved 2025 © Sirotin Enterprises Inc.
Proudly powered by WordPress | Theme: Doo by ThemeVS.