Checkbox and Radio button operations is very simple to perform like as click operation. Checkbox is always recommended to check if that is already in selected mode or deselected. Because, if it is already selected and when you click on the same element it will get deselected. We will look into different ways to perform select and de-select operations on checkboxes. Radio Button is selected by default or anything.
Page HTML Code
<html>
<head>
<title>Checkbox and radio button test page</title>
</head>
<body>
<form name="myform" action="" method="POST">
<div align="center"><br>
<input type="checkbox" name="option1" value="Bike">Bike
<input type="checkbox" name="option2" value="Car">Car
<input type="checkbox" name="option3" value="Plane" checked>Plane
<input type="checkbox" name="option4" value="Truck">Truck
<input type="checkbox" name="option5" value="Milk">Milk
<input type="checkbox" name="option6" value="Butter">Butter
<input type="checkbox" name="option7" value="Cheese">Cheese
<br>
<input type="radio" name="group2" value="Water"> Water
<input type="radio" name="group2" value="Beer"> Beer
<input type="radio" name="group2" value="Wine" checked> Wine
</div>
</form>
</body>
</html>
Demo Source Code
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.firefox.FirefoxDriver;
public class CheckboxandRadiobuttonhandler {
public static void main(String[] args) throws InterruptedException {
// create objects and variables instantiation
WebDriver driver = new FirefoxDriver();
// maximize the browser window
driver.manage().window().maximize();
// launch the firefox browser and open the page
driver.get("file:///C:/Users/Hiro%20Mia/Desktop/Blog%20content/checkbox.html");
//Set timeout
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Get the count of all available check boxes
List<WebElement> number_of_checkbox = driver.findElements(By.tagName("input"));
//Select all checkbox and Radio if selectable
for (int i = 0; i < number_of_checkbox.size(); i++) {
//Checking Checkbox
if (number_of_checkbox.get(i).getAttribute("type").trim().equalsIgnoreCase("checkbox")) {
//Print the Check box count with value
System.out.println("CheckBox = " + i + " " + number_of_checkbox.get(i).getAttribute("value").trim());
//Select if aleady not selected
if (!(number_of_checkbox.get(i).isSelected())) {
number_of_checkbox.get(i).click();
} else {
//De-select the checkbox
number_of_checkbox.get(i).click();
}
} //Checking Radio button
else if (number_of_checkbox.get(i).getAttribute("type").trim().equalsIgnoreCase("radio")) {
// //Print the Check box count with value
System.out.println("Radio = " + i + " " + number_of_checkbox.get(i).getAttribute("value").trim());
//Select if aleady not selected
if (!(number_of_checkbox.get(i).isSelected())) {
number_of_checkbox.get(i).click();
}
}
// Pause so that you can see the results.
Thread.sleep(3000);
}
//close firefox browser
driver.close();
}
}
No comments:
Post a Comment