selenium
  1. selenium-handling-radio-buttons

Handling Radio Buttons - ( Selenium WebDriver )

Radio buttons are a group of HTML elements that allows users to choose only one option from given set of options. In Selenium WebDriver, handling radio buttons is a common practice, as different web applications have their own radio button implementations. In this tutorial, we will learn how to handle radio buttons using Selenium WebDriver.

Syntax

The syntax for finding a radio button using Selenium WebDriver is:

webdriver.find_element_by_name("name_of_radio_group")

Example

from selenium import webdriver

# Create a new instance of the browser driver
driver = webdriver.Chrome()

# Open the URL
driver.get("https://demosite.executeautomation.com/Account/Register")

# Find the radio button using name_of_radio_group
radio_button = driver.find_element_by_name("Gender")

# Click the radio button
radio_button.click()

# Close the browser
driver.quit()

Output

As a result of executing the above code, the radio button with the name 'Gender' will be clicked.

Explanation

In the above example, we have created a script to handle a radio button by finding it using its 'name' attribute. Then, we have clicked on the radio button using the click() method.

We can also locate radio buttons with other attributes such as id, xpath, class name, and tag name.

Use

Radio buttons are commonly used in web forms, where users are requested to select only one option from a given set of choices. By using Selenium WebDriver, we can handle radio buttons on such web forms and automate the selection process.

Important Points

  • Radio buttons are HTML elements that allow users to choose only one option from a given set of choices.
  • Selenium WebDriver provides various techniques to locate radio buttons such as name, id, xpath, class name, and tag name.
  • We can handle radio buttons using the click() method.
  • Radio buttons are commonly used in web forms.

Summary

In this tutorial, we learned how to handle radio buttons using Selenium WebDriver. We saw an example of how to find and click a radio button, and we discussed some important points related to radio buttons. Selenium WebDriver provides a variety of techniques to locate radio buttons, and we can use these techniques to automate the selection process on web forms.

Published on: