Python Selenium Quick Start Guide


In this article, we will see how we can write automated tests with Python from scratch. Here is the list of our requirements to do that so:

  • Python [obviously :)]
  • Selenium WebDriver
  • An IDE (Not a must but nice to have)

If you have all or some of them already you can move on to the next topic.

Installing Python

Though some IDEs might come with Python included, I find it useful to learn to install it separately, since you might some another time like to use it for another purpose.

To install Python on Windows, go to https://www.python.org/downloads/ and click on the latest version available.

python

You also can install Python2.x and use it in your test automation framework but I will be using the latest version throughout the tutorial.

After clicking on download save the file and run it once it is installed (preferably as administrator).

Check the ‘Add Python to Path’ option so you do not have to add it manually and then click ‘Install Now’ and python will be installed automatically. Click ‘Close’ to complete installation afterward.

If everything is ok until now, we can start Python on the command line.

Open up a new command line by typing ‘cmd’ (or ‘command prompt’) after clicking ‘windows’ button.

Type ‘python’, and Python should run.

You can type ‘exit()’ to close Python or close the command prompt directly.

Now we can install Selenium Webdriver.

Installing Selenium Webdriver

Though there are various ways for downloading Selenium Webdriver, in this tutorial I will download it via ‘pip’.

Pip is a package management system used to install and manage software packages written in Python. [1] We have installed pip while we were installing Python.

Open a command prompt again and type ‘pip install selenium’ and click enter.

That’s it! Pretty easy, isn’t it?

Let’s install an IDE to write our first automated test!

Installing PyCharm

Although there are numerous IDEs for Python, I opted PyCharm.

PyCharm is an IDE developed by JetBrains and is one of the best IDEs for Python.

For downloading it go to https://www.jetbrains.com/pycharm/ and click ‘Download Now’.

Click on ‘Download’ under the ‘Community’ title since the community is the free version of this IDE.

Save the file and then run the installer. Click ‘Next’ until you end up with below screen:

If you want to add PyCharm to your desktop check either 32-bit or 64-bit version and you can also create associations with .py so all Python files will be opened with PyCharm. Once the installation is checked ‘Run PyCharm Community Edition’ and we can start writing our first test.

After PyCharm is opened, click on ‘Create New Project’.

Set the name of your project and click ‘Create’.

Right-click on project folder, then hover on new and click on ‘Python file’ to add new Python file to the project.

Name it as you wish and then click ‘OK’.

First, since we are going to write a test we need to import built-in unittest library by typing:

import unittest

Then we need to import Selenium Webdriver:

from selenium import webdriver

Now we are ready to write our class. Our class needs to inherit the TestCase class.

class FirstTest(unittest.TestCase):

Now we can write our first test method inside the class. Please note that there are no annotations for tests in Python like in Junit or Nunit. The name of your methods have to start with test so the interpreter will understand it as a test.

Run your test by selecting the ‘Run’ tab on the top menu and ‘Run Unittests’ option.

You just run your first test! You will see the results below.

We expand our test class by adding setup and teardown methods as below:

Congratulations! You just created your first test!

[1] – https://en.wikipedia.org/wiki/Pip_(package_manager)

javafx

Database Operations in JavaFX

By Onur Baskirt / Apr 1, 2016 / 56 Comments
Before started this section, please check the first article and learn How to Start JAVAFX! http://www.swtestacademy.com/getting-started-with-javafx/ At first, part of JavaFX tutorial series, we created a sample JavaFX project, designed the draft version of the UI and set up an...
rest assured

REST API Testing with Rest Assured

By Onur Baskirt / Mar 8, 2016 / 32 Comments
Outline In this post, I will explain what is API and API testing, what is the difference between SOAP and REST services, and how to test REST APIs with Rest Assured Library. What is API? API stands for Application Programming...
extentreports

How to Write Effective CSS Locators

By Onur Baskirt / Oct 1, 2017 / 0 Comments
Hi all, in this tutorial, I will describe you how to write effective CSS locators to interrogate web elements for your automation projects. As a rule of thumb, your interrogation strategy should be in below order: First try to use...
extentreports

Selenium-11: Execute JavaScript with JavascriptExecutor

By Onur Baskirt / Jan 27, 2016 / 15 Comments
Outline Sometimes we cannot handle some conditions or problems with Webdriver, web controls don't react well against selenium commands. In this kind of situations, we use Javascript. It is useful for custom synchronizations, hide or show the web elements, change...
javafx

Getting Started with JavaFX

By Onur Baskirt / Mar 25, 2016 / 0 Comments
When I started to work in my current position, one of my task is to do manual operations for campaign products  every week. After the second week, I thought that I have to automate this task using a GUI based...
extentreports

How to Write Smart XPath Locators

By Onur Baskirt / Sep 24, 2017 / 4 Comments
Hi all, in this tutorial, I will describe you how to write smart and non-brittle XPath locators. When we write our test scripts, we generally prefer to use id, name, class, etc. these kinds of locators. However, sometimes we could not...
extentreports

Selenium-1: Quick Start to Automation with Selenium WebDriver & JAVA & JUnit & Maven & IntelliJ

By Onur Baskirt / Sep 8, 2015 / 26 Comments
Outline Selenium Webdriver is the most popular open source web test automation framework across wide range of browsers and platforms. In this tutorial you will learn how to do web test automation with Selenium Webdriver and the related tools. Audience...
page object model

Page Object Model with C#

By Ege Aksoz / Jun 18, 2017 / 8 Comments
In the previous tutorial, we’ve taken the initial steps and entered the world of automated testing. We also wrote our first automated test. From this point on, since we are not just going to write one test, we need to...
extentreports

How to Select a Date From DatePicker Using Selenium

By Onur Baskirt / Aug 13, 2016 / 6 Comments
When you need to automate a airway, hotel, or similar websites you need to deal with Datepickers and some times it is a little bit cumbersome to select a specific date on the Datepicker or calendar.  In this post, I...
extentreports

Selenium Webdriver Performance testing with Jmeter and Selenium Grid

By Ozgur Kaya / Aug 9, 2016 / 2 Comments
In this post, we will complete Selenium Webdriver Performance testing scenario using Jmeter and Selenium Grid. 1- Install Java 7 or later If necessary https://java.com/tr/download/ 2- Download latest Jmeter version 3.0 or higher. http://jmeter.apache.org/download_jmeter.cgi 3- Download Jmeter PluginsManager JAR file and...
By | 2017-04-10T15:03:38+00:00 April 3rd, 2017|Selenium, Test Automation|0 Comments

About the Author:

Ege Aksoz
Holding a BSc in Mechatronics, Ege loves to automate. He is now working as a Software Test Automation Engineer at Tallinn, Estonia. He is also passionate about Artificial Neural Networks and he is a master student on AI at Tallinn Technical University.

Leave A Comment