Skip to main content

Handle radio button & Check Boxes in Selenium

The main difference between Radio button and Checkbox is that, using radio button we will be able to select only one option from the options available. whereas using checkbox, we can select multiple options.

Selenium WebDriver supports Radio Button and Radio Group controls using the WebElement class. We can select and deselect the radio buttons using the click() method of the WebElement class and check whether a radio button is selected or deselected using the isSelected() method.

Before performing the click on the Radio buttons or check boxes we will have to verify follwing scenarios :-
  • If Radio button or Checkbox is displayed on the webpage
  • If Radio button or Checkbox is enabled on the webpage
  • Check the default selection of the Radio button or Checkbox
We use predefined methods present in selenium to handle check Box and Radio  button :-
  • isDisplayed()
  • isEnabled()
  • isSelected()

1. isDisplayed() this method returns a Boolean value, if it returns true then said webelement (radio) is present on the webpage or it returns False.

2. Using  isEnabled() this method  returns a Boolean value, if it returns true then said webelement (radio) button is enabled on the webpage or it returns False.

3. Using isSelected() method you can check that the element is selected or not.


@Test
public void testRadioButton()
{
//Get the Radiobutton as WebElement using it's value attribute

WebElement petrol = driver.findElement(By.xpath("//input[@value='Petrol']"));
 
//Check if its already selected? otherwise select the Radiobutton
//by calling click() method

if (!petrol.isSelected())
petrol.click();
//Verify Radiobutton is selected
assertTrue(petrol.isSelected());
//We can also get all the Radiobuttons from a Radio Group in a list
//using findElements() method along with Radio Group identifier

 
List<WebElement> fuel_type = driver.findElements(By.name("fuel_type"));
for (WebElement type : fuel_type)
{
//Search for Diesel Radiobutton in the Radio Group and select
//it
if(type.getAttribute("value").equals("Diesel"))
{
if(!type.isSelected())
type.click();
assertTrue(type.isSelected());
break;
}
}
}

Comments

  1. Nice blog..Good effort explain how to handle radio button in selenium..It was really informative..Thank you..Refer this page also Top selenium training institutes in chennai

    Selenium Testing Course
    software testing course in velachery chennai

    ReplyDelete
  2. SEGA Online Casino - Play at SEA's Leading Casino
    SEGA Online Casino - 카지노사이트 Play 샌즈카지노 at SEA's Leading Casino. Discover the thrill of online casino games right here on your PC. We have over 100's งานออนไลน์ of slot machines

    ReplyDelete
  3. Sharpen your skills and stay ahead in the energy industry with focused oil and gas training — Oilandgas.training

    ReplyDelete
  4. Hospital administrators direct healthcare services, manage staff and finances, ensure regulatory compliance, and improve patient outcomes.
    Join today

    ReplyDelete
  5. Hospital administrators shape healthcare policy, lead operational success, foster teamwork, and ensure quality care across all services.
    Learn more

    ReplyDelete
  6. Hospital administrators plan and implement healthcare services, manage departmental coordination, and ensure seamless, quality-driven patient care.
    Explore more

    ReplyDelete
  7. Hospital administrators lead service teams, manage daily operations, uphold healthcare standards, and ensure patients receive timely, effective care.
    Learn more

    ReplyDelete
  8. A Django training institute provides expert guidance and mentorship.It focuses on real application development.This Django training institute supports career advancement.It is dependable.

    ReplyDelete
  9. Nice read! Hands-on boomi training
    make learning integration tools fast, practical, and career-oriented.

    ReplyDelete
  10. Very helpful information! Hands-on devops engineer course
    practice strengthens deployment, testing, and cloud management expertise.

    ReplyDelete
  11. Insightful post! Our google cloud certification courses
    provide practical, job-oriented training with real-time projects. Prepare for GCP certifications while mastering Compute Engine, Kubernetes, BigQuery, networking, storage, and security to build strong cloud computing skills for a successful IT career.

    ReplyDelete
  12. Great post. Professionals preparing for global certification opportunities frequently choose MuleSoft certification training to strengthen both theoretical knowledge and practical implementation skills.

    ReplyDelete
  13. Great post! The Best Online Power BI Training allows learners to master data analysis, reporting, and visualization concepts from anywhere. Quality training programs provide hands-on projects that help bridge the gap between theory and practice.best power bi training online

    ReplyDelete

Post a Comment