Getting Started with Selenium using Python

Simplified
4 min readAug 9, 2018

--

If you are entering the world of QA software engineering, chances are that you already know Selenium WebDriver is one of the popular tool for web testing and test automation tool out there.

In this short note, I am going to walk you through to show how easy it is to use the Selenium WebDrive Python package to write test scripts to test web applications.

I assume that you have some knowledge of programming and Python (You don’t have to be an expert!).

What is web UI automation?

You can test a web application by manually filling forms, clicking buttons and links, and so on. It works, but does not work well in a real software development scenario.

When a new feature is added or a bug is fixed, you need to run all the tests again to make sure that everything is working as expected and there are no surprises. In the testing world we all this testing as regression testing. Manual regression testing is not only tedious and error prone, but also it is time consuming. UI automation solves this problem. You write the scripts once and test as many time as you want by simply running the scripts.

UI automation allows you to perform all actions you perform manually using a programmable API. You can fill forms, handle pop-ups, click through links, check the results, and so on, while you are sitting comfortably away from your laptop sipping a cup of coffee.

What is Selenium WebDriver? What does it do?

Selenium WebDriver can automate all the above UI testing tasks. It can interact with all types of Web browsers available to date like Firefox, Internet Explorer, Safari, and Chrome, etc.

Let me walk you through how you can use it by showing you a simple example.

This note assumes that you are using either Mac OS or Linux. However, you should be able to do the same on Windows or other operating systems with little or minimal changes. Also, we are using Python here. (Selenium webdriver is available in other programming languages.)

Assuming that you have already installed Python 3.x in your machine, let’s install the selenium package:

pip install selenium

Now we’re ready to write our first program. We are going to automate the task of login to Twitter. I assume that you already have a Twitter account or you should consider creating one — it’s only a few textboxes and clicks away.

I have added ample comments into the following code so that it is easier for you to follow.

#Import web driver from selenium package
#webdriver module implements the classes that support different
#browsers including Chrome, Safari, Firefox and IE.

from selenium import webdriver
#Import Keys from selenium package
#It contains a set of spacil key codes. For example, ADD, ALT,
#TAB, ENTER and RETURN.

from selenium.webdriver.common.keys import Keys
#Used to keep the browser open
import time
#Your twitter username/email and password
#Left blank - you need to fill them with your credentials.

user = "type_your_email"
pwd = "type_your_password"
#this opens a new Firefox browser
#The browser object provides a programmable interface to
#communicate with the Firefox browser using Selenium functions.

browser = webdriver.Firefox()
#open http://www.twitter.com URL
#Webdriver waits until the page is rendered in the browser and
#sends the control back to the Python script.

browser.get("http://www.twitter.com")
#asserting that it loaded Twitter page. Notice that twitter title
#starts with Twitter.

assert "Twitter" in browser.title
#Find the username/email text box by its name
#Twitter uses the name "sesson[username_or_email]"
#If Twitter changes this name, you will have to update your
#script accordingly.

elem = browser.find_element_by_name("session[username_or_email]")
#Fill the username/email text box
elem.send_keys(user)
#find the password text box by its name
elem = browser.find_element_by_name("session[password]")
#Fill the password text box
elem.send_keys(pwd)
#Press enter to submit the login form
elem.send_keys(Keys.RETURN)
#Keep the browser open for 10 seconds
time.sleep(10)
#close the browser
browser.close()

If you get the following error, it means that you either don’t have the geckodriver or the driver is not in the path.

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
stdin=PIPE)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1326, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver'

You can download the geckodriver from here.

Extract it to a location of your choice (say /user/home/mylibs/geckodriver):

tar -xvzf geckodriver-v0.21.0-macos.tar.gz

Add to the $PATH environment variable:

export PATH=$PATH:/user/home/mylibs

You are good to go now. Notice how your Python script opens a new Firefox browser, go the URL http://www.twitter.com, fill in username and password, and finally viola — you are logged into the Twitter account (of course, provided that you enter valid username and password in the above code)!

Happy testing!

References:

https://www.techbeamers.com/selenium-webdriver-python-tutorial/

https://www.guru99.com/selenium-python.html

--

--

Simplified

Anything related to data analysis, QA testing and requirements analysis