In the last article we have discussed about locators in Selenium in great detail. Building on that knowledge, this article will focus on how to interact with Web Elements in Selenium. This article provides an in-depth exploration of handling input fields, buttons, dropdowns, radio buttons, and capturing screenshots using Selenium with Python. By the end of this guide, you’ll have a solid foundation in these essential skills.
Table of Contents
- Interacting with Input Fields and Buttons
- Handling Dropdowns and Radio Buttons
- Taking Screenshots
- Hands-on Practice
Interacting with Input Fields and Buttons
Input Fields
Input fields are fundamental elements in web forms, allowing users to enter various types of data. Here’s how to interact with them using Selenium and Python:
- Locating Input Fields: Use appropriate selectors (ID, name, CSS, XPath) to find the input field.
- Entering Text: Use the
send_keys()
method to type text into an input field. - Clearing Input: Use the
clear()
method to remove existing text from an input field. - Getting Input Value: Retrieve the current value of an input field using the
get_attribute("value")
method.
Example:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome() # Assuming you're using Chrome
# Locate and interact with an input field
input_field = driver.find_element(By.ID, "name")
input_field.send_keys("JohnDoe")
input_value = input_field.get_attribute("value")
input_field.clear()
Buttons
Buttons are interactive elements that trigger actions when clicked. Here’s how to work with buttons:
- Locating Buttons: Use appropriate selectors to find the button element.
- Clicking Buttons: Use the
click()
method to simulate a button click. - Checking Button State: Verify if a button is enabled or disabled using the
is_enabled()
method.
Example:
submit_button = driver.find_element(By.TAG_NAME, "button")
if submit_button.is_enabled():
submit_button.click()
Handling Dropdowns and Radio Buttons
Dropdowns (Select Elements)
Dropdowns, or select elements, allow users to choose from a list of options. Here’s how to interact with them:
- Locating Dropdowns: Find the select element using appropriate selectors.
- Selecting Options: Use the
Select
class fromselenium.webdriver.support.ui
to interact with dropdown options. - Getting Selected Option: Retrieve the currently selected option.
Example:
from selenium.webdriver.support.ui import Select
dropdown = Select(driver.find_element(By.ID, "country"))
dropdown.select_by_value("UNITED STATES")
selected_option = dropdown.first_selected_option
selected_text = selected_option.text
Radio Buttons
Radio buttons allow users to select one option from a group. Here’s how to work with them:
- Locating Radio Buttons: Find radio button elements using appropriate selectors.
- Selecting a Radio Button: Use the
click()
method to select a radio button. - Checking Selection State: Use the
is_selected()
method to verify if a radio button is selected.
Example:
radio_button = driver.find_element(By.ID, "male")
if not radio_button.is_selected():
radio_button.click()
Taking Screenshots
Screenshots are valuable for debugging and documenting test results. Here’s how to capture screenshots with Selenium and Python:
- Full Page Screenshot: Capture the entire visible area of the web page.
- Element Screenshot: Capture a specific element on the page.
Example:
# Full page screenshot
driver.save_screenshot("full_page_screenshot.png")
# Element screenshot
element = driver.find_element(By.ID, "output")
element.screenshot("element_screenshot.png")
Hands-on Practice
To reinforce your understanding of these concepts, we’ve prepared an interactive HTML page for you to practice on. Visit the practice page and try out the following exercises:
- Enter your name in the input field and clear it.
- Select different options from the dropdown menu.
- Choose a radio button option and verify your selection.
- Click the submit button and observe its behavior.
- Capture the screenshot of the output generated after clicking submit button.
By practicing these interactions, you’ll gain practical experience in working with web elements using Selenium and Python.
Remember, mastering these skills takes time and practice. Keep experimenting with different demo websites and scenarios to build your expertise in interacting with web elements using Selenium.
If you are new here, you should start with What is Selenium?