Getting started with selenium on Python

Selenium is a powerful tool that simplifies and automates web testing. It is the UI automation framework of choice among millions of developers, primarily for its simplicity and fluidity. In this article, I will cover setting up selenium to use with Python, and writing a simple script.

For this article, I will assume both pip and python are installed. If you don’t have these installed on your computer, you can find links here: install pip, install python

We first install selenium by opening a terminal and running the following command:

pip install selenium

To make sure selenium is installed, open Python through your terminal by running python , or opening the IDLE application.

Now type the following two commands:

import selenium
from selenium import webdriver

If both of them run without causing any errors, then your selenium installation is complete!

Installing other dependencies

One other dependency we need to use selenium is a webdriver for the browser you will be using. In this article, I am using Chrome, but the process remains similar for Firefox, Safari, or the majority of other browsers available.

Get started by finding the version of your browser you are using. For Chrome, open a new tab, enter the url chrome://version . You’ll see the version of Chrome that you have at the top of the page.

Head to the chromedriver downloads page, and install the version that corresponds to the version of Chrome that you have installed.

These are all the dependencies we’ll be needing to start working with selenium. We can now begin writing our scripts!

Writing your first script

We begin each selenium script by importing the selenium library for python, as well as the webdriver Class.

from selenium import webdriver

This imports the Webdriver class from Selenium. Next, we’ll need to create a webdriver object. We do this by adding the following line:

driver=webdriver.Chrome(executable_path="path\to\your\chromedriver")

After creating our webdriver object, we can tell it to navigate to our website using the following lines of code:

url = "https://www.google.com"
driver.get(url)

Save and run this code. A chrome window should open, navigate to https://www.google.com, and close.

You’ve just written your first selenium script with Python!

In my next article, I’ll cover using selenium to write more advanced scripts and fetch data.

Thanks for Reading!

Senior at Cupertino High School with a few years of experience in python, java, and UI technologies. Currently a software engineering intern at a tech startup.