Have a question?
Message sent Close
View Categories

Handling the radio button and checkbox using selenium

📄
filename.js
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();

        }

    }

}

📄
filename.js
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();

        }

    }

}