Skip to content

Selenium

homepage-banner

Introduction

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 (https://sites.google.com/chromium.org/driver/)
  • Edge (https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/)
  • Firefox (https://github.com/mozilla/geckodriver/releases)
  • Safari (https://webkit.org/blog/6900/webdriver-support-in-safari-10/)

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.

driver.get("https://www.python.org")

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.

print(driver.title)

The output is the following text:

Welcome to Python.org

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.clear()
search_bar.send_keys("getting started with python")
search_bar.send_keys(Keys.RETURN)

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

print(driver.current_url)

The following string is displayed:

'<https://www.python.org/search/?q=getting+started+with+python&submit=>'

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

driver.close()

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 “selenium_test.py” and run “python selenium_test.py” to execute the test.

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

driver = webdriver.Chrome('./chromedriver')
driver.get("https://www.python.org")print(driver.title)
search_bar = driver.find_element_by_name("q")
search_bar.clear()
search_bar.send_keys("getting started with python")
search_bar.send_keys(Keys.RETURN)print(driver.current_url)
driver.close()

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:

driver.switch_to_window('window_name')

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:

print(driver.window_handles)

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.

driver.switch_to_default_content()

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 selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
try:
element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.ID, "id-of-new-element")))finally:
driver.quit()

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.

driver.implicitly_wait(5)
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
        driver.get("https://www.python.org")
        self.assertIn("Python", driver.title)
        elem = driver.find_element_by_name("q")
        elem.send_keys("getting started with python")
        elem.send_keys(Keys.RETURN)
        assert "https://www.python.org/search/?q=getting+started+with+python&submit=" == driver.current_url

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.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.

Leave a message