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.

Interacting with Web Elements in Selenium

Table of Contents

  1. Interacting with Input Fields and Buttons
  2. Handling Dropdowns and Radio Buttons
  3. Taking Screenshots
  4. 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:

  1. Locating Input Fields: Use appropriate selectors (ID, name, CSS, XPath) to find the input field.
  2. Entering Text: Use the send_keys() method to type text into an input field.
  3. Clearing Input: Use the clear() method to remove existing text from an input field.
  4. 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:

  1. Locating Buttons: Use appropriate selectors to find the button element.
  2. Clicking Buttons: Use the click() method to simulate a button click.
  3. 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:

  1. Locating Dropdowns: Find the select element using appropriate selectors.
  2. Selecting Options: Use the Select class from selenium.webdriver.support.ui to interact with dropdown options.
  3. 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:

  1. Locating Radio Buttons: Find radio button elements using appropriate selectors.
  2. Selecting a Radio Button: Use the click() method to select a radio button.
  3. 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:

  1. Full Page Screenshot: Capture the entire visible area of the web page.
  2. 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:

  1. Enter your name in the input field and clear it.
  2. Select different options from the dropdown menu.
  3. Choose a radio button option and verify your selection.
  4. Click the submit button and observe its behavior.
  5. 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?