CheckBox:
The checkbox is a GUI element that allows users to select either single or multiple choices out of the given list.
How to recognize checkbox in HTML?
- We can define a checkbox in HTML using input type="checkbox" tag.
- Any locator strategy that uses DOM for locating web elements should use this tag and properties for recognizing the checkbox.
Locating checkbox by id:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class CheckBox {
public static void main(String[] args) {
WebDriver ch = new ChromeDriver(); // finding element by id
ch.get("https://testautomationpractice.blogspot.com/?utm_source=chatgpt.com");
ch.findElement(By.id("sunday")).click();
boolean tuesdayCheckbox = ch.findElement(By.id("tuesday")).isSelected();
ch.findElement(By.id("monday")).click();
System.out.println(tuesdayCheckbox + "Tuesday check box not selected");
}
}

LOCATE USING CSSSELECTOR AND SELECTING ALL CHECKBOXES USING FINDELEMENTS() METHOD:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class CheckBox_css {
public static void main(String[] args) {
WebDriver ch = new ChromeDriver();
ch.get("https://testautomationpractice.blogspot.com/?utm_source=chatgpt.com");
List<WebElement> allCheckBoxes = ch.findElements(By.cssSelector("input.form-check-input[type='checkbox']"));
System.out.println(allCheckBoxes.size());
for (WebElement value : allCheckBoxes) {
System.out.println(value.getAttribute("value"));
if (!value.isSelected()) {
value.click();
}
}
}
}
LOCATE CHECKBOX USING XPATH:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Checkbox_xpath {
public static void main(String[] args) {
WebDriver ch = new ChromeDriver(); // finding element by id
ch.get("https://testautomationpractice.blogspot.com/?utm_source=chatgpt.com");
List<WebElement> allCheckBoxes = ch
.findElements(By.xpath("//input[@class='form-check-input' and @type='checkbox']"));
System.out.println(allCheckBoxes.size());
for (WebElement value : allCheckBoxes) {
System.out.println(value.getAttribute("value"));
if (!value.isSelected()) {
System.out.println("not selected");
value.click();
}
}
}
}
SELECTING ONLY PARTICULAR CHECKBOXES USING ARRAY
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Checkbox_slectedValuesArray {
public static void main(String[] args) {
WebDriver ch = new ChromeDriver();
ch.get("https://testautomationpractice.blogspot.com/?utm_source=chatgpt.com");
String toSelectid[] = { "sunday", "monday", "wednesday", "friday" };
for (String id : toSelectid) {
System.out.println(id);
if (!ch.findElement(By.id(id)).isSelected())
ch.findElement(By.id(id)).click();
}
}
}
SELECTING ONLY PARTICULAR CHECKBOXES USING ARRAY AND FINDELEMENTS
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Checkbox_xpath {
public static void main(String[] args) {
WebDriver ch = new ChromeDriver();
String toSelectid[] = { "sunday", "monday", "wednesday", "friday" };
ch.get("https://testautomationpractice.blogspot.com/?utm_source=chatgpt.com");
List<WebElement> allCheckBoxes = ch
.findElements(By.xpath("//input[@class='form-check-input' and @type='checkbox']"));
System.out.println(allCheckBoxes.size());
for (WebElement value : allCheckBoxes) {
System.out.println(value.getAttribute("id"));
for(int i=0;i<toSelectid.length;i++) {
if (!value.isSelected()&& value.getAttribute("id").equals(toSelectid[i])) {
System.out.println("not selected");
value.click();
}
}
}
}
}


Top comments (0)