Browser Automation

Browser Automation

Python Selenium browser automation

Browser Automation is a technique where software runs tests on other software.

Automate Chrome browser with Python and Selenium on Windows

Browser automation saves time. This guide will demonstrate how to get going with Selenium Web driver using Python and Chrome browser on Windows O/S.

Other popular browsers have driver support for Selenium and Selenium can be driven from other programming languages. The same scripts can be run with different browsers by importing the relevant driver in your script.

Here is what you need to get started . Get automating now.

You will need:


Install Python

  1. Create a directory on C: drive ‘Py’
  2. Download and install Python v3.x into Py directory

Install Selenium

  1. Open command prompt CMD with administrator rights
  2. C:\Py\scripts\pip install -U selenium

Install Chrome browser

  1. Download and install Chrome browser here

Install Chrome web browser driver – chromedriver.exe

  1. Download Chromedriver
  2. Copy chromedriver.exe to C:\py

Create a browser automation script

Open Python editor IDLE from the Python directory \Lib\idlelib\idle.bat and create a new file and copy and paste the following code. Save as your chosen file name.

The script uses ‘unittest’ to print test results to the Python console. When Python runs it skips text after the # symbol. The comments help explain and label the script to show method and expected outcomes of the test. Well commented script is easier to maintain.

import unittest
import re
from selenium import webdriver
from import WebDriverWait
from selenium.webdriver.common.keys import Keys

# create a class and include unittest
class GetStarted(unittest.TestCase):

    # setup and create a new Chrome browser session
    def setUp(self):
        # Open Chrome browser
        self.driver = webdriver.Chrome()
        # Maximise browser window
        # Navigate
    def test_1_search(self):
        """Test that the search text is found on the results page"""
        # select search box
        self.elem = self.driver.find_element_by_class_name("search-field")
        # Clear the search box of any text

        # Type 'software' into search box - search text

        # hit the Enter key to search
        # search function operates and loads a new page with search results
        # Test that the word 'software' is displayed - results page
        src = self.driver.page_source
        text_found ='software', src)
        self.assertNotEqual(text_found, None)
    # Close the browser and selenium driver
    def tearDown(self):

# print test results to the Python console
if __name__ == '__main__':

Run the script.

From the IDLE file menu select Run or press F5 to run the script.


The script will open the browser and run a search in Graft Computing Test Tips for the word ‘software’. When the search results are loaded into a new page the script checks that the word ‘software’ is on the page. A simple test that is likely to pass and display this in the Python console as follows:

Ran 1 test in 13.178s


Now edit the script to verify the presence on the search result page, a text string that you wouldn’t expect to find e.g. ‘qwerty’. The test will fail and the result is displayed in the Python console as follows:

FAIL: test_1_search (__main__.GetStarted)
Test that the search text is found on the results page
Traceback (most recent call last):
  File "drive:\folder\", line 40, in test_1_search
    self.assertNotEqual(text_found, None)
AssertionError: None == None

Ran 1 test in 12.062s

FAILED (failures=1)
note how the text between """ in the script is also a comment and displayed as the title for the failure.

Explore different ways of working with Python and Selenium Web driver. Mastering a few operations and assertions can go a long way to cover typical user interactions with a browser to automate customer web journeys and verify expected behaviour. Manual test case steps can be used to build automated test cases, building a useful script library of common script to reuse in other projects.

F12 opens browser tools. Use this to find elements and copy their properties to paste into your scripts.

Browser dev tools to find element ID's.
Get started quickly with this ready made script to use Python and Selenium to automate a web browser to run a test, then verify and display the result.

There are plenty of code editors available. Idle is light to use and can run scripts directly from the editor which keeps things simple for now.

It will take time and effort to master browser automation. You will grow a library of script you can re use to help with the next project tasks. Progress is very satisfying. Saving bags of time in the long run and reducing manual repetitive tasks.

Gradft Computing Test Tips
Graft Computing Test Tips

Copyright © Graft Computing Ltd 2019

Tags: ,