Automated testing Python Selenium WebDriver

Selenium WebDriver with Python – initial configuration

So, you want to start working with Selenium WebDriver with Python. In this post I will tell you what you need to do at the very beginning.

Install Selenium WebDriver

In order to be able to call WebDriver methods and classes from your python code you need to install selenium module

pip install selenium

You can install the module into a virtual environment. Read the post Python virtual environment with venv to understand the advantages of this approach.

Selenium drivers for browsers

You will use Selenium WebDriver to manipulate a browser. It is possible to run selenium tests on any popular browser. Such as Chrome, Firefox, Safari, Opera, Edge, etc. There is a driver for each of these browsers. The driver can listen commands from selenium and pass them to the browser. The list of drivers with the download links you can find on the official page.

Driver location

Important update. Now Selenium does not require chromedriver, geckodriver, edgechromiumdriver. If you have Selenium version 4.6.0 or higher you can skip the part about driver download and installation. In the version 4.6.0 Selenium introduced Selenium Manager which now configures the browser drivers for Chrome, Firefox, and Edge. And you don’t need to do this by yourself.

You can find more details in this post: Now you don’t need to install chromedriver.

Unpack the downloaded driver and place into any directory listed in you computer’s PATH. That’s the first option.

The second option is to add to PATH the directory where you want to store the driver.

And the third option is to specify the path to the driver in the python code where you will use the driver. For example, for Chrome it will look like

from import Service

service = Service(executable_path='/home/user1/Downloads/chromedriver')

driver = webdriver.Chrome(service=service)

First of all you import the Service class. Then you initialize this class with the “executable_path” argument where you specify the full path to the driver file. And finally you use the initialized Service class instance when you are initializing the driver.

Initialize the driver

Before you give any command to the browser you need to initialize the WebDriver. Each browser has it’s own class inside the selenium library.

from selenium import webdriver

chrome_driver = webdriver.Chrome()
ie_driver = webdriver.Ie()
firefox_driver = webdriver.Firefox()

Change browser’s window size

Almost all sites have adaptive design nowadays. By default, selenium opens browser with a small window. Often you will need to change the window’s size to the maximum to be able to see the full version of the web application. There are different options to change the size of the browser’s window. The first one is to call the driver’s method to maximize the window.

driver = webdriver.Chrome()

The second option is to start driver with options. And set an option to start in maximized size.

from import Options

options = Options()
driver = webdriver.Chrome(options=options)

And the third option lets you to set the size of the window manually. Furthermore that is the only working option for Mac users.

from import Options

options = Options()
driver = webdriver.Chrome(options=options)

So if you are a Mac user and you need to maximize the browser’s window set this size to the maximum size of your screen.

Open an URL in browser

To tell the driver what URL it should open in the browser use the “get” method of the WebDriver.


Close browser

To close the window you’ve previously opened use “quit” method.


Close a tab

To close a browser’s tab you are currently in use “close” method.


If you run the selenium scripts in the Chrome browser and there is only one opened tab, this command will close the browser entirely.

Get basic information from the browser

After opening a browser and a page you can request some information to ensure you are on the right page.

Current URL

To get the URL of the current page use “current_url” method of the WebDriver.


You can print the value or make an assert that the URL is the expected one.

Page source

In order to get the source code of the current page use the “page_source” method.


Page title

To get the title of the current page use “title” method.


All the method listed here return a string with the information you requested.

By Eugene Okulik