How to Handle CheckBox and Radio Button in Selenium WebDriver

How to Handle CheckBox and Radio Button in Selenium WebDriver

What we have learned so far?

Part 1 – What is Selenium WebDriver? How to setup Selenium?


Part 2 – Locators in Selenium WebDriver – Id, Name, Class Name, Tag Name, CSS, XPath

https://www.onlyfullstack.com/locators-in-selenium-webdriver/

Part 3 – Selenium WebDriver Commands

How to select Radio Button in Selenium WebDriver?

How to select CheckBox in Selenium WebDriver?

Radio Buttons or CheckBox can be toggled by using the click() method.

We can refer below link to check the radio buttons and checkboxes :
https://onlyfullstack.blogspot.com/2020/03/best-demo-website-to-practice-selenium.html

public class RadioButtonsAndCheckboxOperations {
    public static void main(String[] args) throws InterruptedException {
        System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, "chrome-web-driver/chromedriver");
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://onlyfullstack.blogspot.com/2020/03/best-demo-website-to-practice-selenium.html");

        webDriver.manage().window().maximize();
        JavascriptExecutor js = (JavascriptExecutor) webDriver;
        js.executeScript("window.scrollBy(0,2200)");


        selectRadioButtonsAndCheckbox(webDriver);
    }

    private static void selectRadioButtonsAndCheckbox(WebDriver webDriver) throws InterruptedException {
        WebElement radioButton = webDriver.findElement(By.id("male"));
        radioButton.click();

        Thread.sleep(1000);

        WebElement singingCheckBox = webDriver.findElement(By.id("hobby1"));
        singingCheckBox.click();

        WebElement playingCheckBox = webDriver.findElement(By.id("hobby2"));
        playingCheckBox.click();

        Thread.sleep(2000);
    }
}

How to check if the Radio Button is selected in Selenium WebDriver?

How to check if the CheckBox is selected in Selenium WebDriver?

we can use the isSelected method to check if the Radio Button or CheckBox is checked or selected or not.

package com.onlyfullstack.selenium.radioButtonsAndCheckbox;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;

public class RadioButtonsAndCheckboxOperations {
    public static void main(String[] args) throws InterruptedException {
        System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, "chrome-web-driver/chromedriver");
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://onlyfullstack.blogspot.com/2020/03/best-demo-website-to-practice-selenium.html");

        webDriver.manage().window().maximize();
        JavascriptExecutor js = (JavascriptExecutor) webDriver;
        js.executeScript("window.scrollBy(0,2200)");

        checkIfRadioButtonsAndCheckboxSelected(webDriver);
    }

    private static void checkIfRadioButtonsAndCheckboxSelected(WebDriver webDriver) {
        WebElement radioButton = webDriver.findElement(By.id("male"));
        WebElement singingCheckBox = webDriver.findElement(By.id("hobby1"));
        WebElement playingCheckBox = webDriver.findElement(By.id("hobby2"));

        System.out.println("Is Male Radio button Selected? = " + radioButton.isSelected());
        System.out.println("Is Singing CheckBox Selected? = " + singingCheckBox.isSelected());
        System.out.println("Is Playing CheckBox Selected? = " + playingCheckBox.isSelected());

        radioButton.click();
        singingCheckBox.click();
        playingCheckBox.click();
        System.out.println("Is Male Radio Button selected after click? = " + radioButton.isSelected());
        System.out.println("Is Singing CheckBox selected after click? = " + singingCheckBox.isSelected());
        System.out.println("Is Playing CheckBox selected after click? = " + playingCheckBox.isSelected());
    }
}

Output-

Is Male Radio button Selected? = false
Is Singing CheckBox Selected? = false
Is Playing CheckBox Selected? = false
Is Male Radio Button selected after click? = true
Is Singing CheckBox selected after click? = true
Is Playing CheckBox selected after click? = true

Lets go to our next tutorial where we will discuss below points :

Part 5 – How to Handle DropDown in Selenium WebDriver

– What is Select in Selenium WebDriver?
– How to select an option from drop-down menu?
– 1. Select By Visible Text
– 2. Select By Value
– 3. Select By Index
– How to get the values from DropDown menu?

Source Code
You can find the complete source code on below GitHub repository – 
https://github.com/onlyfullstack/selenium-tutorial

Selenium Tutorial
https://www.onlyfullstack.com/selenium-webdriver-tutorial/