Select CheckBox & Radio Button in Selenium WebDriver 2021

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

5/26/2021 Select CheckBox & Radio Button in Selenium WebDriver 2021

INTERVIEW QUESTIONS JAVA JAVA PROGRAMS TEST CASES SELENIUM MANUAL TESTING DIFFERENCE

SoftwareTestingo » Selenium » Selenium Tutorial » How to Select CheckBox and Radio Button in Selenium WebDriver

How to Select CheckBox and Radio Button in Selenium


WebDriver Search this website
Last Updated On: September 1, 2020 By Softwaretestingo Editorial Board

Tutorials Important Links

Software Testing Jobs

Manual Testing Tutorial

Selenium Tutorial

Core Java Tutorial

TestNG Tutorial

Java Programs

What We Are Learn On This Post  Selenium Programs

Manual Test Cases


Select CheckBox & Radio Button in Selenium WebDriver: In this automation world, its quite common
that when you are trying to automate Web Form that time, you can see that a web form contains Interview Tips
Checkboxes and Radio buttons. These elements enhance the functionality of the web page by providing
Contact US
facilities like selecting single and multiple values.
www.softwaretestingo.com
Until this tutorial, we have seen how we can locate the various type of elements with the help of different
locator strategies. Still, in this tutorial, we are going to learn how we can perform operations on
Checkboxes and radio buttons. There are few new operations we need to verify on these two elements Important Links
like is the checkbox is already checked, or the radio button is already selected and verify their default
states. Software Testing Interview Questions

Agile Interview Questions


How to Select CheckBox & Radio Button
We can try to learn the various possible ways to select the elements with real-time examples and also Manual Testing Interview Questions
verify the current status. So that you can understand easily.
Testing Interview Questions
Read Also: Selenium Wait Commands
Selenium Interview Questions
For Selecting any element (Check Box / Radio button) you need to click on the element, so the command
for this operation it looks like below: Selenium Real Time Interview Questions

// Java code example to select checkbox/radio button. Selenium WebDriver Interview Questions
WebElement target = driver.findElement(By.id("checkbox1"));
Automation Framework Interview Questions
target.click();
Appium Interview Questions
https://www.softwaretestingo.com/select-checkbox-radio-button-in-selenium/ 1/6
5/26/2021 Select CheckBox & Radio Button in Selenium WebDriver 2021

When ID Of Each radio Button Is Unique


TestNG Interview Questions
package com.selenium.practice.checkboxRadiobutton;
Java Interview Questions
import java.util.concurrent.TimeUnit;
Core Java Interview Questions
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
Categories
import org.openqa.selenium.chrome.ChromeDriver;

public class RadiobuttonWithUniqueID Select Category


{
public static void main(String[] args) throws InterruptedException
{
WebDriver driver;
System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

//When The Checkbox/Radio button have an Unique ID


driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-
radio-button.html");
driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);

driver.findElement(By.id("python")).click();
System.out.println("Python Selected");
Thread.sleep(5000);
driver.close();
}
}

When Multiple Elements Have the Same ID


package com.selenium.practice.checkboxRadiobutton;

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class RadiobuttonWithoutUniqueID


{
public static void main(String[] args) throws InterruptedException
{
WebDriver driver;
System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

//When The Checkbox/Radio button have an Unique ID


driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-
radio-button.html");
driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);

List<WebElement> checkBoxes=
driver.findElements(By.xpath("//input[@id='checkbox']"));
System.out.println("Select Ruby Radio Button: "+checkBoxes.size());
for(WebElement options : checkBoxes)
{
String linkName = options.getAttribute("value");
if (linkName.equals("Selenium"))
{
options.click();
break; // Exit from the loop, this is important
}
}
Thread.sleep(5000);
https://www.softwaretestingo.com/select-checkbox-radio-button-in-selenium/ 2/6
5/26/2021 Select CheckBox & Radio Button in Selenium WebDriver 2021
driver.close();
}
}

How to Deal with CheckBox


package com.selenium.practice.checkboxRadiobutton;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class CheckBoxEx


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

WebDriver driver;
System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

//When The Checkboxes have an Unique ID


driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-
radio-button.html");
driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);

//Select Singing CheckBox


driver.findElement(By.id("sing")).click();
Thread.sleep(3000);
System.out.println("Singing Check Box Selected");
driver.close();
}
}

How to Check the state Of Radio Button Or Checkboxes?


Sometimes before doing any action, we need to check the state of the Radio button or checkbox, and
according to their state, we decide what operation we have to perform. So to verify the state of the
element, we can use the IsSelected() method. When we execute the IsSelected() method, it will return a
boolean value like true or false. Based on that, we can get to know the state of the element like if it is
true, then that means the element is selected, and if it is false, then that means that specific element is
not selected.

It’s better to verify a few things before performing any operation on the elements, like:

If Radio button or Checkbox is displayed on the webpage


If Radio button or Checkbox is enabled on the webpage
Check the default selection of the Radio button or Checkbox

We can verify these things as mentioned earlier with the help of the below-predefined methods in
selenium webdriver:

isDisplayed(): This method returns a boolean value. if it returns true, Which means the web element is
displaying on the web page. If the element is not displaying on the web page then it will return a Boolean
value false.

WebElement eleBtn = driver.findElement (By.id("testing"));


eleBtn.isDisplayed();

package com.selenium.practice.checkboxRadiobutton;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

https://www.softwaretestingo.com/select-checkbox-radio-button-in-selenium/ 3/6
5/26/2021 Select CheckBox & Radio Button in Selenium WebDriver 2021
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class IsDisplayedEx


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

WebDriver driver;
System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

//When The Checkboxes have an Unique ID


driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-
radio-button.html");
driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);

//Locate The Element


WebElement singing=driver.findElement(By.id("sing"));
//If Element is Displaying Then Return True else It Will Return False
Boolean CheckboxValue=singing.isDisplayed();
if(CheckboxValue)
{
System.out.println("Singing Is Displpaying On WebPage");
}
else
System.out.println("Singing Is Not Displaying In the Webpage");
driver.close();
}
}

isEnabled(): This method return an boolean value. This method is used to verify that a web element is
enabled or disabled within a web page.

WebElement eleBtn = driver.findElement (By.id("testing"));


eleBtn.isEnabled;

package com.selenium.practice.checkboxRadiobutton;

import java.util.concurrent.TimeUnit;

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

public class isEnabledEx


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

WebDriver driver;
System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

//When The Checkboxes have an Unique ID


driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-
radio-button.html");
driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);

//Locate The Element


WebElement singing=driver.findElement(By.id("sing"));
//If Element is Enabled Then Return True else It Will Return False
Boolean CheckboxValue=singing.isEnabled();
if(CheckboxValue)

https://www.softwaretestingo.com/select-checkbox-radio-button-in-selenium/ 4/6
5/26/2021 Select CheckBox & Radio Button in Selenium WebDriver 2021
{
System.out.println("Singing Is Enabled");
}
else
System.out.println("Singing Is Disabled");
driver.close();
}
}

isSelected(): IsSelected() method is used to verify the other element is selected or not. If the element is
selected then this method will be returned boolean value true else you will be getting a false value.

WebElement eleBtn = driver.findElement (By.id("testing"));


eleBtn.isSelected();

package com.selenium.practice.checkboxRadiobutton;

import java.util.concurrent.TimeUnit;

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

public class isSelectedEx


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

WebDriver driver;
System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

//When The Checkboxes have an Unique ID


driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-
radio-button.html");
driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);

//Locate The Element


driver.findElement(By.id("python")).click();
WebElement pythonElement=driver.findElement(By.id("python"));
//If Element is Selected Then Return True else It Will Return False
Boolean RadiobuttonValue=pythonElement.isSelected();
if(RadiobuttonValue)
{
System.out.println("Pyhton Is Selected");
}
else
System.out.println("Python Is Not Selected");
Thread.sleep(2000);
driver.close();
}
}

https://www.softwaretestingo.com/select-checkbox-radio-button-in-selenium/ 5/6
5/26/2021 Select CheckBox & Radio Button in Selenium WebDriver 2021

Filed Under : Selenium Tutorial

Leave a Reply
Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

POST COMMENT

Copyright © 2021 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy

https://www.softwaretestingo.com/select-checkbox-radio-button-in-selenium/ 6/6

You might also like