How to handle check box in Selenium Webdriver

asked 3 months ago

There are different operations that can be performed on a checkbox using Selenium WebDriver. The most common operations include:

           -  Check / Uncheck
           -  Determine if the checkbox is Editable or not Editable
           -  Check whether the checkbox is Selected or Not Selected

Navigate to https://www.qafeast.com/demo, and click the checkbox tab.

Press F12 on the keyboard and inspect the checkbox.

 

 

Check/uncheck:

Selenium webdriver doesn't provide a special method to select the checkbox, we can use the click() method to select the radio button.

WebElement checked = driver.findElement(By.name("reportchkbox_1"));
checked.click();

 

Editable or not:

Checkbox may not be editable sometimes, to determine whether it is editable or not, we can use the isEnabled() function

WebElement checked = driver.findElement(By.name("reportchkbox_1"));
boolean isSelected = checked.isEnabled();
if (isSelected) {
     System.out.println("Check box is Enabled and Editable");
}else{
    System.out.println("Check box is not Enabled and Editable");
}

 

Selected or not selected:-

To verify which check box is checked, we can use the isSelected() function

WebElement checked = driver.findElement(By.name("reportchkbox_1"));
boolean isSelected = checked.isSelected();
if (isSelected) {
    System.out.println("Check box is selected");
}else{
    System.out.println("Check box is not selected");
}
 

 

 Using the getAttribute() function we can determine using the checked property, if the checkbox is checked then it returns true or null if it is not checked

String rdBt = driver.findElement(By.name("reportchkbox_1")).getAttribute("checked");
System.out.println(rdBt );

 

Using Selenium Csharp

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace ConsoleApp1
{
    internal class SeleniumTest
    {
        static IWebDriver driver;
        static void Main(string[] args)
        {
           
            driver = new ChromeDriver(@"\\Driver\\chromedriver.exe");
            //Implicit Wait:
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            driver.Navigate().GoToUrl("https://qafeast.com/demo");
            driver.FindElement(By.XPath("//label[text()='Checkbox']")).Click();
           //Check/uncheck:
            driver.FindElement(By.Name("reportchkbox_1")).Click();
          //Editable or not:
            bool isEnabled = driver.FindElement(By.Name("reportchkbox_1")).Enabled;
            if (isEnabled)
            {
                Console.WriteLine("Check box is Enabled and Editable");
            }
            else
            {
                Console.WriteLine("Check box is not Enabled and Editable");
            }
            //Selected or not selected :-
            bool isSelected = driver.FindElement(By.Name("reportchkbox_1")).Selected;
            if (isSelected)
            {
                Console.WriteLine("Check box is selected");
            }
            else
            {
                Console.WriteLine("Check box is not selected");
            }
          //  Using getAttribute() function 
           string rdBt = driver.FindElement(By.Name("reportchkbox_1")).GetAttribute("checked");
           Console.WriteLine(rdBt);
            driver.Quit();  
        }
    }
}