Check if the Element is Selected
This is an additional step, in case you want to check if the checkbox is selected or not and then perform the certain action. An element that is checked or not can be tested using the isSelected function.
isSelected()
The function that is used to check if a checkbox or radio button element is selected or not is known as the isSelected function. It returns a boolean value.
Java
//Import selenium libraries import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.By; public class selenium4 { public static void main(String[] args) { // State the chromedriver URL System.setProperty( "webdriver.chrome.driver" , "C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe" ); // Define and initiate the chrome driver WebDriver driver = new ChromeDriver(); // Open the Geeks For Geeks website driver.get( "https://www.w3wiki.org/explore?page=1&sortBy=submissions&itm_source=w3wiki&itm_medium=main_header&itm_campaign=practice_header" ); // Maximize the screen driver.manage().window().maximize(); // Click on the pop-up driver.findElement(By.xpath( "/html/body/div[1]/div/div[3]/div[2]/div/div[2]/div/div/div/button" )).click(); // Finding the checkbox 'Amazon' WebElement check_box1 = driver.findElement(By.xpath( "// *[contains(text(),'Amazon')]" )); // Finding the checkbox 'Flipkart' WebElement check_box2 = driver.findElement(By.xpath( "// *[contains(text(),'Flipkart')]" )); // Clicking the checkbox 1 check_box1.click(); // Printing the state of both checkboxes System.out.println(check_box1.isSelected()); System.out.println(check_box2.isSelected()); } } |
Output:
true
false
Selenium Webdriver Handling Checkbox using Java
A set of tools and libraries that allow the user to automate interactions with web browsers is known as Selenium. It is too diverse tool that can handle every operation of a webpage starting from opening the webpage to closing of webpage. In this article, we will see the various steps to handle the checkboxes using Selenium webdriver in Java.
Contact Us