Learn

Selenium and Python: A detailed tutorial

The importance of software testing cannot be overstated. It helps you make sure that your software meets requirements and is free of defects. And automating tests is a powerful way to ensure an app’s reliability, functionality, and scalability.

 

Python and Selenium are two popular choices for automated testing. Python is an open-source, general-purpose, high-level programming language with a rich collection of libraries that powers a vast number of applications across industries. Selenium, on the other hand, was explicitly designed to automate web browsers and test web applications. Together they’re a dynamic duo, combining the strengths of both tools. Python’s concise syntax and extensive libraries make it an ideal companion for Selenium, enabling testers to create robust and efficient automated test scripts for web applications.

Author:

Guest Contributors

Date: Jan. 18, 2024

In this article, you’ll gain a comprehensive understanding of Selenium and Python integration for automation testing. You’ll learn how to set up a testing environment, write effective test scripts, handle common automation challenges, and leverage the power of Selenium and Python for efficient testing workflows.

The best way to acquire a true understanding of any language or technology is with hands-on experience.

Setting up Selenium with Python

The best way to acquire a true understanding of any language or technology is with hands-on experience, so get ready to set up your system for automation testing with Python and Selenium. Be sure to set up Python and Python Package Manager (pip). For writing code, we’ll use Jupyter Notebook.

Installing Selenium

The first step is installing the Selenium Python package by accessing your command prompt or terminal and executing the following pip command:

$ pip install selenium

Installing a web driver

Additionally, Selenium requires a web driver, such as ChromeDriver for Google Chrome, to establish smooth interactions with your preferred browser. The Selenium website offers a repository of various other web driver options for different needs. You can also use the Python code to download these drivers. First, download the chromedriver-autoinstaller package to install the Chrome web driver.

$ pip install chromedriver-autoinstaller

Once the package is downloaded, you can open Jupyter Notebook and write the following lines of code to install it:

# For Chrome
# import dependency
import chromedriver_autoinstaller
# install webdriver
chromedriver_autoinstaller.install()

Configuration and environment setup

Creating an optimized Python environment is critical. Begin by creating a new Python file and naming it test_script.py by importing the essential libraries:

from selenium import webdriver

Note: it is preferred (not mandatory) to have a separate Python virtual environment or Anaconda environment for different projects.

Handling web driver drivers and paths

It’s imperative that you manage paths according to your operating system. This guarantees that the web driver is seamlessly accessible to your Python scripts. As an illustration, consider setting the path for the ChromeDriver:

# Windows Example
driver_path = ‘C:/path/to/chromedriver.exe’

# macOS/Linux Example
driver_path = ‘/path/to/chromedriver’

Note: be sure to replace the path with the actual location of your web driver for proper functioning.

With these steps, you’ve successfully set up Selenium with Python, installed a web driver, configured your environment, and handled the web driver paths. Now you’re ready to start writing automated test scripts!

Basic Selenium Concepts

WebElements and locators

WebElements is the cornerstone of Selenium. It serves as the bridge between your scripts and the elements on a web page. These elements mostly consist of functionalities like buttons, forms, links, and more. Anchoring these elements are locators, which grant the means to accurately identify them. This arsenal includes the following:

  • ID
  • Class name
  • Name
  • Tag name
  • Link text
  • Partial link text
  • XPath
  • CSS selector

These examples showcase how to use different locators to identify web elements using Selenium. Choosing the right locator strategy depends on the structure of the web page and the specific element you want to interact with.

ID locator

If you have the HTML Element: <input id=”username”> on your website, you can locate it using the find_element_by_id() method as follows:

Selenium Code: element = driver.find_element_by_id(“username”)

Class name locator

If you have the HTML Element: <button class=”submit-button”>Submit</button>, you can locate it using the class name as follows:

Selenium Code: element = driver.find_element_by_class_name(“submit-button”)

Interactions with WebElements

Selenium generously provides the tools to interact with WebElements, effortlessly executing actions like clicks, typing, selections, and more.

See the examples below:

# Clicking a button
element.click()

# Typing in an input field
element.send_keys(“Text to type”)

Navigating and managing windows and tabs

Selenium also allows you to open, close, switch between, and handle multiple windows and tabs.

# Opening a new tab
driver.execute_script(“window.open(”, ‘_blank’);”)

# Switching between tabs
driver.switch_to.window(driver.window_handles[1])

# Closing the current tab
driver.close()

Handling alerts and pop-ups

Alerts, prompts, and confirmations are commonplace in JavaScript. Selenium provides the means to interact with these through dedicated methods.

# Handling an alert
alert = driver.switch_to.alert

# Accepting the alert
alert.accept()

Selenium also allows you to handle more complex alerts that require text input. This is particularly useful for scenarios where user authentication or confirmation is required.

# Handling a Prompt
prompt = driver.switch_to.alert

prompt.send_keys(“Hello, World!”)
prompt.accept()

Implementing wait mechanisms

Waits in Selenium ensure that the web driver waits for a certain condition before proceeding. This is crucial when dealing with dynamic web content or slower-loading pages.

# import selenium dependencies
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Waiting for an element to be clickable
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, “element_id”))
)

With these basic concepts, you should now have a solid foundation to start automating interactions with web pages using Selenium. Remember to apply these principles in your test scripts for effective automation testing.

Writing your first Selenium script

Getting started with Selenium involves setting up your testing environment, which includes installing Selenium, configuring web drivers, and setting up your Python environment.

A test script in Selenium is a set of instructions written in Python that automates the actions a user would perform on a webpage.

Creating a test script

A test script in Selenium is a set of instructions written in Python that automates the actions a user would perform on a webpage. These scripts are executed by the Selenium web driver, which interacts with the browser.

Opening a web page

In Selenium, you can open a web page using the get() method of the web driver class.

# Opening a webpage
driver.get(“https://www.example.com”)

Locating web elements (single or multiple)

Locating web elements is important when you want to interact with them. Selenium provides various locators, including ID, class name, name, tag name, link text, partial link text, XPath, and CSS selector. To locate an element by its ID, you can use the script below.

# Using ID locator
element = driver.find_element_by_id(“element_id”)

And to locate multiple elements, you can use this one:

# Using class name locator for multiple elements
elements = driver.find_elements_by_class_name(“element_class”)

Interacting with WebElements

Once you’ve located a web element, you can interact with it by performing actions like clicks, typing, selections, etc.

# Clicking a button
element.click()

# Typing in an input field
element.send_keys(“Text to type”)

Browser commands

Selenium allows you to control the browser itself. This includes maximizing the window, going back, refreshing the page, and more.

# Maximizing the window
driver.maximize_window()

# Going back to the previous page
driver.back()

Navigation commands

Selenium enables you to navigate through web pages. You can navigate forward, refresh, get the current URL, and more.

# Navigating forward
driver.forward()

# Getting the current URL
current_url = driver.current_url

By understanding and combining these essential concepts, you’ll be able to create powerful automated test scripts using Selenium and Python. Remember to apply these principles in your test scripts for effective automation testing.

Conclusion

Now that you know how to master interactions, handle alerts, and implement strategic wait mechanisms, you’re ready to tackle even the most complex testing scenarios.

This post was written by Gourav Bais. Gourav is an applied machine learning engineer skilled in computer vision/deep learning pipeline development, creating machine learning models, retraining systems, and transforming data science prototypes to production-grade solutions.

Author:

Guest Contributors

Date: Jan. 18, 2024