Selenium with Python Tutorial
Selenium is a powerful open-source automation tool widely used for web application testing. It allows developers and testers to automate browser actions, interact with web elements, and perform various tasks, making it an essential tool for web automation. In this tutorial, we will explore Selenium with Python, a popular programming language for test automation, and provide a step-by-step guide to getting started with Selenium using Python.
Not only does Selenium offer a wide range of functionalities for web automation, but it also boasts a vibrant ecosystem of frameworks and libraries that further enhance the testing experience. Some of the renowned Selenium Python frameworks are: PyTest, Robot Framework and Behave.
This post provides an introduction to Selenium with Python tutorials, covering essential topics such as installation, writing your first script, element identification, and handling various interactions. In the upcoming series, we will delve deeper into utilising frameworks to create advanced automation scripts with Selenium and Python.
Related reads: How to Integrate Jenkins with Selenium
Setting up the Environment
In this section, we will explore the process of setting up the environment for Selenium with Python. This involves installing Python, Selenium, and the required WebDrivers to facilitate smooth integration and execution. By following the steps outlined in this section, you will be equipped with a properly configured environment to embark on your Selenium automation journey using Python. Let’s dive in and ensure everything is set up correctly for seamless development and testing with Selenium and Python.
Installing Python
- Visit the official Python website (https://www.python.org) and download the latest version of Python for your operating system.
- Run the downloaded installer and follow the instructions.
- Make sure to check the box that says “Add Python to PATH” during the installation process.
- Verify the installation by opening a command prompt or terminal and running the command python –version. It should display the installed Python version.
Installing Selenium
- Open a command prompt or terminal.
- Run the following command to install Selenium using pip:
pip install selenium |
pip will download and install the Selenium library and its dependencies.
Installing WebDrivers
- Selenium requires specific WebDrivers to interact with different browsers. You need to download and install the appropriate WebDriver for the browser you intend to automate.
- The most common browsers used with Selenium are Chrome, Firefox, and Safari, here we will show the steps with the most common browser: Chrome.
a. Installing ChromeDriver (for Google Chrome):- Visit the ChromeDriver download page (https://sites.google.com/a/chromium.org/chromedriver/downloads).
- Download the appropriate version of ChromeDriver that matches your installed version of Google Chrome.
- Extract the downloaded file.
- Move the extracted chromedriver executable to a directory that is included in your system’s PATH variable.
Writing Your First Selenium Script
In this section, we will write our first demonstration script using Selenium with Python. We strongly encourage you to run this script on your local setup before proceeding with the remaining sections of this blog. This will ensure that your environment is correctly configured and that you can successfully execute Selenium scripts using Python. So, let’s dive in and get started with running our initial script to verify that everything is set up correctly for your Selenium-Python development environment.
In this example, we’ll use the website “https://qa-practice.netlify.app/bugs-form.html” and the scenario is to open the URL and then validate the title, after validation we have to quit the driver.
from selenium import webdriver # Create a new instance of the Chrome driver driver = webdriver.Chrome() # Navigate to the website driver.get(“https://qa-practice.netlify.app/bugs-form.html”) # Validate the title expected_title = “Bug Report Form” actual_title = driver.title if expected_title == actual_title: print(“Title validation successful!”) else: print(“Title validation failed. Expected:”, expected_title, “Actual:”, actual_title) # Close the browser driver.quit() |
Make sure you have installed the Selenium library and the appropriate WebDriver (in this case, ChromeDriver) as mentioned in the previous instructions. Save the above code in a Python file (e.g., first_script.py) and execute it. It will open the website, validate the title, and print whether the title validation was successful or not. Finally, it will close the browser.
Related: Selenium WebDriver Tutorial
Feel free to explore more Selenium capabilities and perform additional actions on the website, such as interacting with elements, submitting forms, or navigating to different pages.
Locating Web Elements
Identifying web elements using appropriate locators is crucial when working with Selenium and Python. While various methods such as ID, Name, Class Name, Link Text, and CSS Selector are effective, it is advisable to prioritise ID and Name over XPath when they are available. Additionally, developers can leverage browser developer tools to assist in identifying elements accurately.
Locator Method | Example |
By ID | element = driver.find_element_by_id(“element-id”) |
By Name | element = driver.find_element_by_name(“element-name”) |
By Class Name | element = driver.find_element_by_class_name(“element-class”) |
By Tag Name | elements = driver.find_elements_by_tag_name(“tag-name”) |
By Link Text | link = driver.find_element_by_link_text(“Text on the link”) |
By Partial Link Text | link = driver.find_element_by_partial_link_text(“Partial Text”) |
By CSS Selector | element = driver.find_element_by_css_selector(“css-selector”) |
By XPath | element = driver.find_element_by_xpath(“xpath-expression”) |
Use these locator methods in your Selenium scripts to locate web elements based on the specific attributes or criteria of the elements you are targeting on your webpage.
Interacting with Web Elements
Interacting with web elements is a crucial aspect when working with Selenium and Python. It allows us to perform various actions in our test scripts. In this section, we will explore essential operations such as clicking elements, entering values into text boxes, handling alerts, pop-ups, dropdowns, and more. Mastering these interactions is fundamental for building effective and comprehensive test scripts with Selenium and Python.
Here are examples of different interactions with web elements using Selenium with Python:
Clicking Elements
# Clicking a button button = driver.find_element_by_id(“button-id”) button.click() # Clicking a link link = driver.find_element_by_link_text(“Click Here”) link.click() |
Sending Keys (Typing into input fields)
# Typing into a text field text_field = driver.find_element_by_id(“text-field-id”) text_field.send_keys(“Hello, World!”) # Sending special keys (e.g., Enter key) text_field.send_keys(Keys.ENTER) |
Clearing Text
# Clearing a text field text_field = driver.find_element_by_id(“text-field-id”) text_field.clear() |
Extracting Text
# Extracting text from an element element = driver.find_element_by_id(“element-id”) text = element.text print(“Extracted Text:”, text) |
Handling Dropdowns
from selenium.webdriver.support.ui import Select # Selecting an option by visible text dropdown = Select(driver.find_element_by_id(“dropdown-id”)) dropdown.select_by_visible_text(“Option 1”) # Selecting an option by value dropdown.select_by_value(“option_value”) # Selecting an option by index dropdown.select_by_index(2) |
Working with Checkboxes and Radio Buttons
# Clicking a checkbox checkbox = driver.find_element_by_id(“checkbox-id”) checkbox.click() # Clicking a radio button radio_button = driver.find_element_by_id(“radio-button-id”) radio_button.click() |
Handling Alerts and Pop-ups
# Accepting an alert alert = driver.switch_to.alert alert.accept() # Dismissing an alert alert.dismiss() # Entering text in an alert alert.send_keys(“Text for the alert”) alert.accept() |
Remember to import the necessary modules/classes and replace the element locators (“element-id”, “button-id”, etc.) with appropriate locators based on the HTML structure of your webpage.
These examples cover some common interactions with web elements using Selenium with Python. Feel free to modify them based on your specific requirements and the structure of the website you are automating.
Related reads: Windows In Selenium WebDriver
Advanced Interactions
This section will provide an in-depth exploration of advanced interactions commonly used in web UI automation with Selenium and Python. We will focus on three significant interactions: Mouse Actions (Drag and Drop, Double Click), Keyboard Actions (Keys, Combining Keys), and Handling Frames and Windows. By mastering these advanced interactions, you will be equipped with essential skills to efficiently automate web applications using Selenium and Python.
Here are examples of advanced interactions using Selenium with Python:
Mouse Actions (Drag and Drop, Double Click)
from selenium.webdriver import ActionChains # Performing drag and drop action source_element = driver.find_element_by_id(“source-element-id”) target_element = driver.find_element_by_id(“target-element-id”) actions = ActionChains(driver) actions.drag_and_drop(source_element, target_element).perform() # Performing double click action element = driver.find_element_by_id(“element-id”) actions = ActionChains(driver) actions.double_click(element).perform() |
Keyboard Actions (Keys, Combining Keys)
from selenium.webdriver.common.keys import Keys # Sending keys to an input field text_field = driver.find_element_by_id(“text-field-id”) text_field.send_keys(“Hello, World!”) # Combining keys (e.g., Ctrl+A to select all text) text_field.send_keys(Keys.CONTROL + “a”) |
Handling Frames and Windows
# Switching to a frame by name or ID driver.switch_to.frame(“frame-name-or-id”) # Switching back to the default content driver.switch_to.default_content() # Switching to a window or tab by handle current_window_handle = driver.current_window_handle window_handles = driver.window_handles # Switching to a new window or tab for handle in window_handles: if handle != current_window_handle: driver.switch_to.window(handle) break |
These examples demonstrate how to perform mouse actions like drag and drop and double-click using ActionChains, send keys to web elements, and handle frames and windows using Selenium with Python. Remember to import the necessary modules (ActionChains and Keys) and adapt the code to match the specific elements and actions required in your automation scenario.
Conclusion
By leveraging the power of Selenium and Python, you now have the ability to automate browser actions, validate web elements, interact with forms, handle alerts and pop-ups, and perform complex testing scenarios. Additionally, we explored advanced interactions like mouse actions, keyboard actions, and managing frames and windows, empowering you to tackle a wide range of web automation challenges.
In the next series on this blog, we will deep dive into handling waits, taking screenshots, handling exceptions, and integrating with popular test frameworks. Remember, Selenium with Python offers a vast array of possibilities for web testing and automation. As you continue your journey, don’t hesitate to explore additional features, experiment with different locators, and adopt best practices to enhance your test scripts. Stay tuned for more in-depth tutorials that will enhance your skills and enable you to build robust and sophisticated automation solutions.