DEV Community

Cover image for Hands-On Selenium with Python: From Basics to Framework Building
Torin Vale
Torin Vale

Posted on

1 1 1 1 1

Hands-On Selenium with Python: From Basics to Framework Building

In today’s fast-paced digital world, automation has become integral to software testing. Selenium is one of the most preferred tools among the various automation tools. Selenium is an open-source tool that efficiently lets you automate web applications. When combined with Python, which is an easy-to-learn and powerful programming language, Selenium becomes even more effective for testing.

This guide is a comprehensive introduction to Selenium using Python which can help you begin with your first automation script.

What is Selenium?

Selenium is an open-source automation framework that helps automate web applications across different browsers and platforms. It offers a suite of tools that include-

  • Selenium WebDriver — Automates interactions with web applications.
  • Selenium IDE — It’s a browser extension that helps in recording and playback of tests.
  • Selenium Grid — It helps run multiple tests in parallel on multiple machines and browsers.

In this article we will see how we can use Python to automate Selenium tests using Selenium WebDriver.

Setting Up Selenium With Python

Before starting to write our automated test scripts, we need to set up our development environment.

Installing Python

First, you need to ensure that Python is installed in your system. You may download its latest version from the official Python website.

To verify the installation, open the terminal or command prompt and enter the below command-

python3 --version
You will see the version of Python installed in your system.

Installing Selenium

Next, you can install the Selenium package using the pip installer. For this, enter the below command in the terminal or command prompt-

pip3 install selenium
You can check the version installed for Selenium using the below command-

pip3 freeze | grep “selenium”

Installing IDE- PyCharm

To start writing Python code, you would need an IDE. We will be using PyCharm Community Edition for this tutorial. You can follow the steps on the official website and download the IDE.

Writing Your First Python Selenium Script

  1. Now that your development environment is ready, you are all set to write your first Python Selenium automation test. We will be writing a basic step where we will perform the following steps-
  • Open Google Chrome.
  • Navigate to www.google.com.
  • Capture the title of the web page.
  • Close the browser driver.

Let us look at the steps to start our first Selenium test script with Python.

  1. Upon launching PyCharm, navigate to File and then click on New Project.
    Next, enter the desired name for the project and click on the Create button. You may also select the Location for your project.

  2. Now you can create your first test in the .py file. All you need to do is right click on your project and click on New and then select Python File.

  3. Once your file is created, you can start writing your first selenium script with Python. Below is the code for the simple test case discussed above.

#Imports required libraries from Selenium
from selenium import webdriver
#Initializing webdriver
driver = webdriver.Chrome()
#Navigating to google.com
driver.get("https://www.google.com")
#Fetching title and printing it
print(driver.title)
#Closing webdriver instance
driver.quit()
Enter fullscreen mode Exit fullscreen mode

Now, run the test and see the results to see the Chrome browser launching, the page title being printed in the console window and finally the browser instance closing down.

Basic Selenium Commands Using Python

Opening a Web Page

We use the basic get() method to open a web page using Selenium.
river.get(“https://www.google.com”)

Locating Web Elements

Actions and interactions on a web page can be performed only after locating the web elements. Suppose an element is defined as –
<input type="text" name="password" id="pass-id" />

Selenium offers various locating strategies as discussed below-

1. Finding elements by ID
Syntax-
driver.find_element(By.ID,"id of the web element")
Example-
driver.find_element(By.ID,"pass-id")

2. Finding elements by name
Syntax-
driver.find_element(By.NAME,"Name of the web element")
Example-
driver.find_element(By.NAME,"password")

3. Finding elements by XPath
Syntax-
driver.find_element(By.XPATH,"XPath of the web element")
Example-
driver.find_element(By.XPATH,"//input[@id=’pass-id’]")

4. Finding elements by CSS Selector
Syntax-
driver.find_element(By.CSS_SELECTOR,"CSS Selector of the web element")
Example-
driver.find_element(By.CSS_SELECTOR,"input#pass-id")

Interacting With Web Elements
Once the elements are located, we can perform actions like clicking, sending input, or retrieving text.

  1. Clicking a button
    element.click()

  2. Entering text
    element.send_keys("Text to be entered")

  3. Retrieving text
    text = element.textprint(text)

Handling Dynamic Elements

In most applications, some web elements appear dynamically, and for interacting with them Selenium needs to wait. To handle such elements, we can use implicit and explicit waits.

1.Implicit Wait

The below code is for the webdriver to wait for 10 seconds before executing the next line of code. Note that once implicit wait is applied, it is set for the life of the webdriver object.
driver.implicitly_wait(10)

2. Explicit Wait

It waits for a defined condition to occur before proceeding further. In the below code, the webdriver will wait for 10 seconds for the element to match the criteria and if no element is found, a Timeout exception will be thrown.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.google.com")
wait = WebDriverWait(driver,10)
element = wait.until(EC.presence_of_element_located(By.ID,"Dynamic Element ID"))
Handling Alerts, Frames and Windows
Handling Alerts
alert = driver.switch_to.alert
alert.accept() #Clicks on OK
alert.dismiss() # Clicks on Cancel
Enter fullscreen mode Exit fullscreen mode

3. Handling Frames
driver.switch_to.frame("Frame name")

4. Handling Windows

handles = driver.window_handles
driver.switch_to.window(handles[1]) # Switch to the second window (index 1), assuming a new tab was opened.
Enter fullscreen mode Exit fullscreen mode

Taking Screenshots

The below code allows you to take a screenshot of the current page.

driver.save_screenshot("Screenshot.png")

Using Pytest With Selenium

PyTest is a Python testing framework that can be used to write your automation test scripts. It helps in efficient test execution and reporting. Let’s have a quick glance of how you can use it for the same test that we wrote above.

Installing PyTest

You can install PyTest on your Python environment by using the below command in the command line or terminal of your Python project.pip install pytest

Writing PyTest Test Case

We will be using the same test case as we used for our first Selenium test using Python. We will just add verifying the page title in this test. The code would look like below-

import pytest
from selenium import webdriver
def test_google_title():
   # Initialising webdriver
   driver = webdriver.Chrome()
   # Navigating to google.com
   driver.get("https://www.google.com")
   #Fetching the page title and asserting its value
   assert "Google" in driver.title
   #Quitting the Webdriver
   driver.quit()
Enter fullscreen mode Exit fullscreen mode

Executing the Test

You need to run the below command in your IDE terminal to execute the test. Note that you can replace your test file name. Additionally, an HTML report will be generated for the test and added to the project path as shown in the screenshots below.

pytest PyTestTestCaseOne.py --html=report.html

Best Practices for Selenium Automation Using Python

Selenium using Python is an effective combination to automate your tests, but it is always better to consider certain best practices to make your tests even more effective. Some of these are-

  • Instead of using time.sleep() or implicit waits, always prefer using explicit waits to synchronize test execution with web page loading.
  • Optimize WebDriver management by avoiding launching a new WebDriver instance for every test. Instead, use PyTest fixtures for efficient test execution.
  • Run tests in headless mode for faster execution.
  • Use Page Object Model (POM) to organize test scripts by separating page elements from the test logic.
  • Leverage Selenium Grid for parallel test execution to run tests on multiple browsers and environments, helping scale your test coverage.
  • Capture screenshots upon test failures.
  • Use logging to log test execution for easy debugging.

Conclusion

Selenium with Python is a powerful combination for test automation. We have covered the basics of Selenium along with Setting up Selenium and Python to start with test automation. We saw basic commands that can help navigate, locate elements, perform actions, handle dynamic elements, frames, windows and alerts using Python with Selenium. By using the PyTest framework you can increase the efficiency of your tests and build a robust test automation framework.

You are now fully equipped to start using Selenium with Python and automate your repetitive web tasks efficiently!

Source: This article was originally published at TestGrid.

Top comments (0)