Skip to content




Selenium is an open-source automation testing tool that supports multiple scripting languages. Python has grown in popularity and is now ranked as the third most popular programming language. With Selenium, you can define tests, detect test results on a chosen browser, and assess the browser’s response to changes.

Pre-requisites to run Selenium Python tests

The installer pip is the easiest way to install Selenium with Python.

pip install selenium

While installing Selenium provides its functionality, you also need to install additional drivers to enable it to interact with your preferred web browser. Below are the download links for the drivers:

  • Chrome (
  • Edge (
  • Firefox (
  • Safari (

For the rest of this Python Selenium tutorial, we will use Chromedriver. Click on the link for your chosen browser and download the driver that matches the compatible version.

If you only intend to test Selenium locally, downloading the package and drivers should be sufficient. However, if you want to set up Selenium on a remote server, you will also need to install the Selenium Server. Selenium Server is written in Java and requires JRE 1.6 or above to be installed on your server. It can be found on the Selenium download page.

Selenium Python example: How to run your first test?

Once you have completed the prerequisites section, you can start your first test in Selenium using the Python programming language!

1. First, import the WebDriver and Keys classes from Selenium.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

The WebDriver class will connect you to an instance of a browser, which we will cover shortly. The Keys class allows you to simulate keyboard key strokes, including special keys like “Shift” and “Return”.

2. Next, create an instance of Chrome using the path of the driver you downloaded from the respective browser’s website. In this example, it assumes the driver is in the same directory as the Python script you will execute.

driver = webdriver.Chrome('./chromedriver')

If you are running the test on your local machine, this will open a local instance of Chrome. You can perform tests on it until you use the .close() method to end the connection to the browser.

3. Next, use the .get() method of the driver to load a website. You can also load a local development site. This process is equivalent to opening a window of Chrome on your local machine, typing a URL, and hitting Enter. The .get() method starts loading a website and waits for it to render completely before moving on to the next step.


4. Once the page loads successfully, you can use the .title attribute to access the textual title of the webpage. If you want to check whether the title contains a specific substring, use the assert or if statements. For simplicity, let’s print the title of the page.


The output is the following text:

Welcome to

If you run the test on a Python interpreter, you will notice that the Chrome browser window remains active. Additionally, a message displayed on Chrome indicates that automated software is currently controlling it.

5. Next, we will submit a query in the search bar. To do this, select the element from the HTML DOM, enter a value into it, and submit the form by emulating the Return key press. You can select the element using its CSS class, ID, name attribute, or tag name. Upon inspecting the source of the query search bar, you will find that the name attribute of this DOM element is “q”. Therefore, you can utilize the .find_element_by_name() method as shown below to select the element.

search_bar = driver.find_element_by_name("q")

6. Once the DOM element is selected, you need to clear its contents using the .clear() method. Then, enter a string as its value using the .send_keys() method. Finally, emulate the press of the Return key using Keys.RETURN.

search_bar.send_keys("getting started with python")

You may have noticed that performing these actions results in a change in the URL, displaying the search results in the window. To confirm the current URL of the


The following string is displayed:


To close the current session and disconnect the link with the browser, use the .close() method.


In this example, we have explored the steps involved in running our first test using Selenium Python. It’s important to note that we kept the window open during all test stages to ensure you could see what was happening in the background as each command was executed.

In a fully automated flow, you would run multiple tests sequentially and may not be able to observe each step as it happens.

To summarize the discussion, here is your first Selenium test on Python. Save it in the file “” and run “python” to execute the test.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('./chromedriver')
search_bar = driver.find_element_by_name("q")
search_bar.send_keys("getting started with python")

After successfully running your first test in Selenium with Python, let’s explore different options to select DOM elements and interact with them. In the example, we selected the search bar and queried for a string. Now, let’s delve deeper into the selection process. Here is the HTML code for the search bar.

<input id="id-search-field" name="q" type="search" role="textbox" class="search-field" placeholder="Search" value="" tabindex="1">

In the example, we used the .find_element_by_name() method to search for the attribute name within the input HTML tag. However, there are other methods available to search for this term.

  • CSS ID: .find_element_by_id(“id-search-field”)
  • DOM Path: .find_element_by_xpath(“//input[@id=’id-search-field’]”)
  • CSS class: .find_element_by_class_name(“search-field”)

While the CSS ID is unique to each element by design, you may find multiple results when searching through the class name. On the other hand, when you search through the DOM path of the element, you can be certain of what you are searching for.

In your web application, you might need to interact with multiple windows and frames. Some common scenarios where you’ll work with new windows include social logins and file uploads. The .switch_to_window() method of the driver allows you to change the active window and perform different actions in the new window. Use the following code to switch focus to a new window:


If the value is not stored in the target attribute, you can use a window handle, which uniquely identifies all open windows in your driver. To view a list of all window handles, execute the following code:


Similarly, you can switch focus to a frame within a window through the .switch_to_frame() method. After completing relevant actions, run the following to switch back to the primary window.


Note that all actions within this section change the driver’s state and do not return anything. Therefore, the values are not stored in a variable, and instead, the methods are called directly.

Working with Idle Time During a Test

While we have discussed various tests in static web applications, a single-page application may require you to wait for a specific time before taking any action.

There are two types of waits in Selenium: implicit and explicit waits.

  • An explicit wait makes the driver wait for a specific action to be completed, such as content load using AJAX.
  • An implicit wait makes the driver wait for a specified amount of time.

For an explicit wait, it is recommended to use a try-finally block to avoid the worst-case scenario of the test getting stuck. Essentially, you instruct the driver to wait for a certain element for a specified time before proceeding.

from import By
from import WebDriverWait
from import expected_conditions as EC
element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.ID, "id-of-new-element")))finally:

First, use the WebDriverWait() function to instruct the driver to wait for five seconds. Then, test for the presence of a new element using the .presence_of_element_located() method from the expected_conditions class, which can be queried through By.ID.

For an implicit wait, use the .implicitly_wait() method of the driver and specify the number of seconds to wait.

element = driver.find_element_by_id("id-of-new-element")

How to integrate Selenium with Python Unit Tests?

Let’s learn how to integrate Selenium tests into Python unit tests using the unit test module in Python.

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

class ChromeSearch(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome('./chromedriver')

    def test_search_in_python_org(self):
        driver = self.driver
        self.assertIn("Python", driver.title)
        elem = driver.find_element_by_name("q")
        elem.send_keys("getting started with python")
        assert "" == driver.current_url

    def tearDown(self):

if __name__ == "__main__":

In this example, you need to set up the driver object by using the .Chrome() method when initializing the unit test class. The provided test enters the specified text into the search bar and compares the resulting change in the URL with the previously seen URL. You can write additional tests for different browsers and reuse the same functionality.


Small world. Big idea!
  • Welcome to visit the knowledge base of SRE and DevOps!
  • License under CC BY-NC 4.0
  • Made with Material for MkDocs and improve writing by generative AI tools
  • Copyright issue feedback, replace # with @