CheckBox & Radio Button Operations in selenium C#
CheckBox & Radio Button Operations in selenium C# are easy to perform and most of the time the simple ID attributes work fine for both of these. But selection and d-selection is not the only thing we want with Check Boxes and Radio Buttons. We might like to check that if the Check Box is already checked or if the Radio Button is selected by default or anything. Check Boxes and Radio Button deals exactly the same way and you can perform below-mentioned operations on either of them.
Different Selection Method
By ID
If ID is given for the Radio Button/CheckBox and you just want to click on it irrespective of it's value, then the command will be like this:
IWebElement radioBtn = driver.FindElement(By.Id("toolsqa"));
radioBtn.Click();
With Selected
If your choice is based on the pre-selection of the Radio Button/Check Box and you just need to select the deselected Radio Button/Check Box. Assume there are two Radio Buttons/Check Boxes, one is selected by default and you want to select the other one for your test. With IsSelected statement, you can get to know that the element is selected or not.
// Store all the elements of same category in the list of WebLements
IList<IWebElement> oRadioButton = driver.FindElements(By.Name("toolsqa"));
// Create a boolean variable which will hold the value (True/False)
bool bValue = false;
// This statement will return True, in case of first Radio button is selected
bValue = oRadioButton.ElementAt(0).Selected;
// This will check that if the bValue is True means if the first radio button is selected
if (bValue == true)
{
// This will select Second radio button, if the first radio button is selected by default
oRadioButton.ElementAt(1).Click();
}
else
{
// If the first radio button is not selected by default, the first will be selected
oRadioButton.ElementAt(0).Click();
}
Note: Name is always the same for the same group of Radio Buttons/Check Boxes but their Values are different. So if you find the element with the name attribute then it means that it may contain more than one element, hence we need to use FindElements method and store the list of WebElements.
With Value
You can even select Radio Buttons/Check Boxes with their Values.
// Find the checkbox or radio button element by Name
IList <IWebElement> oCheckBox = driver.FindElements(By.Name("tool"));
// This will tell you the number of checkboxes are present
int Size = oCheckBox.Count;
// Start the loop from first checkbox to last checkboxe
for (int i = 0; i < Size; i++)
{
// Store the checkbox name to the string variable, using 'Value' attribute
String Value = oCheckBox.ElementAt(i).GetAttribute("value");
// Select the checkbox it the value of the checkbox is same what you are looking for
if (Value.Equals("toolsqa"))
{
oCheckBox.ElementAt(i).Click();
// This will take the execution out of for loop
break;
}
}
By CssSelector
A simple way of selecting a check-box or radio button is by using its value:
IWebElement oCheckBox = driver.FindElement(By.CssSelector("input[value='Tools QA']"));
oCheckBox.Click();
Practice Exercise
- Launch new Browser
- Open "https://demoqa.com/automation-practice-form/"
- Challenge One - Select the deselected Radio button (female) for category Sex (Use Selected method)
- Challenge Two - Select the Third radio button for category 'Years of Exp' (Use Id attribute to select Radio button)
- Challenge Three - Check the Check Box 'Automation Tester' for category 'Profession'( Use Value attribute to match the selection)
- Challenge Four - Check the Check Box 'Selenium IDE' for category 'Automation Tool' (Use CssSelector)
Solution
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ToolsQA.Selenium_Basics
{
class CheckboxAndRadioButtonOperations
{
[Test]
public void Test()
{
// Create a new instance of the Firefox driver
IWebDriver driver = new FirefoxDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
// Launch the URL
driver.Url = "https://demoqa.com/automation-practice-form";
// Step 3 : Select the deselected Radio button (female) for category Sex (Use IsSelected method)
// Storing all the elements under category 'Sex' in the list of WebLements
IList<IWebElement> rdBtn_Sex = driver.FindElements(By.Name("sex"));
// Create a boolean variable which will hold the value (True/False)
Boolean bValue = false;
// This statement will return True, in case of first Radio button is selected
bValue = rdBtn_Sex.ElementAt(0).Selected;
// This will check that if the bValue is True means if the first radio button is selected
if (bValue == true)
{
// This will select Second radio button, if the first radio button is selected by default
rdBtn_Sex.ElementAt(1).Click();
}
else
{
// If the first radio button is not selected by default, the first will be selected
rdBtn_Sex.ElementAt(0).Click();
}
//Step 4: Select the Third radio button for category 'Years of Exp' (Use Id attribute to select Radio button)
IWebElement rdBtn_Exp = driver.FindElement(By.Id("exp-2"));
rdBtn_Exp.Click();
// Step 5: Check the checkbox 'Automation Tester' for category 'Profession'( Use Value attribute to match the selection)
// Find the checkbox or radio button element by Name
IList<IWebElement> chkBx_Profession = driver.FindElements(By.Name("profession"));
// This will tell you the number of checkboxes are present
int iSize = chkBx_Profession.Count;
// Start the loop from first checkbox to last checkboxe
for (int i = 0; i < iSize; i++)
{
// Store the checkbox name to the string variable, using 'Value' attribute
String Value = chkBx_Profession.ElementAt(i).GetAttribute("value");
// Select the checkbox it the value of the checkbox is same what you are looking for
if (Value.Equals("Automation Tester"))
{
chkBx_Profession.ElementAt(i).Click();
// This will take the execution out of for loop
break;
}
}
// Step 6: Check the checkbox 'Selenium IDE' for category 'Automation Tool' (Use cssSelector)
IWebElement oCheckBox = driver.FindElement(By.CssSelector("input[value='Selenium IDE']"));
oCheckBox.Click();
// Kill the browser
driver.Close();
}
}
}