Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Selenium Automation Testing

  • Introduction to Automation Testing
  •  Introduction to Selenium
  • Advantages and Disadvantages of Selenium WebDriver
  •  Locators in Selenium
  • How to launch the browsers ? 
  • Browser Navigation Commands in Selenium
  •  Handling the textbox and button using selenium
  • Handling the radio button and checkbox using selenium
  •  Handling the drop downs and multi-select dropdown
  • Validate the text validation and attributes values. 
  • Handling Alert Popup
  • Handling iFrames
  • Handling Multiple Browsers or Tabs
View Categories
  • Home
  • Tutorials
  • Selenium Automation
  • Selenium Automation Testing
  • Handling the radio button and checkbox using selenium

Handling the radio button and checkbox using selenium

Handling the radio button and

checkbox using selenium

Handling Radio Button 

Test Case Flow: 

  1. Launch Chrome via WebDriver.
  2. Navigate to https://example.com/preferences.
  3. Locate the radio button using XPath //input[@type=’radio’ and @value=’male’].
  4. Check if it is displayed, enabled, and not already selected
  5. Select the radio button if it isn’t already
  6. Verify the radio button is now selected
  7. Close the browser.

Program:

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class RadioButtonExample {

    public static void main(String[] args) throws InterruptedException {

        // 1. Point to your ChromeDriver executable

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        // 2. Launch Chrome

        WebDriver driver = new ChromeDriver();

        try {

            // 3. Navigate to the page containing your radio buttons

            driver.get("https://example.com/preferences");

            // 4. Locate the desired radio button using an XPath locator

            //    (adjust the XPath to match your page)

            WebElement maleRadio = driver.findElement(

                By.xpath("//input[@type='radio' and @value='male']"));

            // 5. Check if it is displayed, enabled, and not already selected

            System.out.println("Radio displayed? " + maleRadio.isDisplayed());

            System.out.println("Radio enabled?   " + maleRadio.isEnabled());

            System.out.println("Initially selected? " + maleRadio.isSelected());

            // 6. Select the radio button if it isn’t already

            if (maleRadio.isDisplayed() && maleRadio.isEnabled() && !maleRadio.isSelected()) {

                maleRadio.click();

                // (demo pause)

                Thread.sleep(2000);

                // 7. Verify the radio button is now selected

                System.out.println("After click, selected? " + maleRadio.isSelected());

            }

        } finally {

            // 8. Close the browser (ends the session)

            driver.quit();

        }

    }

}

Handling Checkbox  

Test Case Flow: 

  1. Launch Chrome via WebDriver.
  2. Navigate to https://example.com/preferences.
  3. Locate the checkbox using an XPath locator
  4. Check if the checkbox is displayed, enabled, and its current state
  5. Select the checkbox if it isn’t already checked
  6. Verify the checkbox is now selected
  7. Uncheck it again
  8. Close the browser.

Program:

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class CheckboxExample {

    public static void main(String[] args) throws InterruptedException {

        // 1. Point to your ChromeDriver executable

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        // 2. Launch Chrome

        WebDriver driver = new ChromeDriver();

        try {

            // 3. Navigate to the page containing your checkbox

            driver.get("https://example.com/terms");

            // 4. Locate the checkbox using an XPath locator

            //    (adjust the XPath to match your page)

            WebElement agreeCheckbox = driver.findElement(

                By.xpath("//input[@type='checkbox' and @id='agreeTerms']"));

            // 5. Check if the checkbox is displayed, enabled, and its current state

            System.out.println("Checkbox displayed?    " + agreeCheckbox.isDisplayed());

            System.out.println("Checkbox enabled?      " + agreeCheckbox.isEnabled());

            System.out.println("Initially selected?    " + agreeCheckbox.isSelected());

            // 6. Select the checkbox if it isn’t already checked

            if (agreeCheckbox.isDisplayed() 

                    && agreeCheckbox.isEnabled() 

                    && !agreeCheckbox.isSelected()) {

                agreeCheckbox.click();

                // (demo pause so you can see it happen)

                Thread.sleep(2000);

                // 7. Verify the checkbox is now selected

                System.out.println("After click, selected? " + agreeCheckbox.isSelected());

            }

            // 8. (Optional) Uncheck it again

            if (agreeCheckbox.isSelected()) {

                agreeCheckbox.click();

                Thread.sleep(1000);

                System.out.println("After uncheck, selected? " + agreeCheckbox.isSelected());

            }

        } finally {

            // 9. Close the browser (ends the session)

            driver.quit();

        }

    }

}

Selenium Automation Testing
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
 Handling the textbox and button using selenium Handling the drop downs and multi-select dropdown
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105