Mastering RadioButton Interactions with Selenium in Python: A Beginner’s Guide
In this lesson, we’re going to explore how to work with radio buttons using Selenium WebDriver in Python, drawing on a practical example from the “Practice Automated Testing” website.
Setting Up the Environment
Before diving into the code, ensure you have the Selenium WebDriver and the WebDriver Manager for Python installed in your environment. These tools are essential for automating browser interactions in your tests. If you haven’t installed them yet, you can do so using pip:
pip install selenium webdriver-manager
Navigating to the Radio Buttons Page
We start with navigating to the Radio Buttons Page on the “Practice Automated Testing” website. This page serves as a playground for practicing Selenium interactions, including working with radio buttons.
Here’s how you can set up your WebDriver and navigate to the page:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
# Set up WebDriver
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
# Navigate to the Radio Buttons Page
driver.get("https://www.practiceautomatedtesting.com/webelements/RadioButtonPage")
print("Opened practiceautomated testing")
Interacting with Radio Buttons
Once on the page, the next step is to interact with a radio button. In our example, we’ll select the first radio button. To do this, we need to locate the element using one of Selenium’s locating strategies, such as By.CSS_SELECTOR
, and then perform a click action on the element.
import time
from selenium.webdriver.common.by import By
# Locate the radio button and click it
inputusername = driver.find_element(By.CSS_SELECTOR, '[id="radio1"]')
inputusername.click()
time.sleep(2)
Verifying the Selection
After clicking the radio button, it’s good practice to verify that the action was successful. One way to do this is to wait for a specific element associated with the radio button selection to be present in the Document Object Model (DOM).
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Wait for the expected text to be present in the DOM
try:
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "[class*='RadioButtons']"))
)
print("Selected the option 1")
except Exception as e:
print("Something went wrong:", e)
Cleaning Up
Finally, after verifying the radio button selection, don’t forget to close the browser window to clean up resources.
# Close the driver
driver.quit()
Conclusion
Automating interactions with radio buttons using Selenium WebDriver in Python is a straightforward process that involves locating the button, performing a click action, and optionally verifying the selection. By practicing with real examples, such as those available on the “Practice Automated Testing” website, you can sharpen your Selenium skills and become proficient in web automation testing.
To dive deeper into Selenium and further your learning, consider exploring more complex scenarios and different web elements. The repository at https://github.com/learn-automated-testing/pythonfortesters provides a wealth of examples, including the one discussed in this post, to help you along your journey in mastering Selenium with Python.