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)

16 thoughts on “Python Selenium Quick Start Guide”

  1. I am unable to run the test code after following the step mentioned in there. please advise about the error:
    at this line: from selenium import webdriver (i guess unable to import seleniumwebdirver)

    Reply
  2. its seems installed successfully before running the script:
    C:\Users\Altaaf>pip install selenium
    Collecting selenium
    Downloading selenium-3.8.1-py2.py3-none-any.whl (942kB)
    100% |████████████████████████████████| 952kB 666kB/s
    Installing collected packages: selenium
    Successfully installed selenium-3.8.1

    Please advise further meanwhile I am reviewing the link you provided

    Reply
  3. C:\Users\Altaaf\PycharmProjects\firstSeleniumTest\venv\Scripts\python.exe C:/Users/Altaaf/PycharmProjects/firstSeleniumTest/firstTest.py
    Traceback (most recent call last):
    File “C:/Users/Altaaf/PycharmProjects/firstSeleniumTest/firstTest.py”, line 2, in
    from selenium import webdriver
    ModuleNotFoundError: No module named ‘selenium’

    Process finished with exit code 1

    Reply
  4. Unable to run the scrip,Below error is displaying after following above steps-

    “C:\Users\harshita.garg\PycharmProjects\first selenium\venv\Scripts\python.exe” “C:/Users/harshita.garg/PycharmProjects/first selenium/first test 1.py”
    Traceback (most recent call last):
    File “C:/Users/harshita.garg/PycharmProjects/first selenium/first test 1.py”, line 2, in
    from selenium import webdriver
    ModuleNotFoundError: No module named ‘selenium’

    Process finished with exit code 1

    Reply
  5. I am getting “AttributeError: module ‘selenium.webdriver’ has no attribute ‘Chrome’ ” error after executing test case.

    Reply
  6. I am getting “AttributeError: module ‘selenium.webdriver’ has no attribute ‘chrome’ ” error after executing script.

    Reply
    • Hi,

      Two possible solutions for this issue are:
      1 – You might missing selenium package. Try ‘pip install selenium’.
      2 – You might add path of the chrome driver: driver = webdriver.Chrome(executable_path=’path/to/chromedriver’)

      Reply
  7. i have the same issue.

    the solution:
    1. Please download chrome driver form selenuim website.
    2. put it in you python path: C:\\AppData\Local\Programs\Python\Python37\Scripts\chromedriver.exe
    3. Add it to the environment variable/ Path.
    4. Setting the Pycharm “Project Interpreter” to your own python environment.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.