Click on an Element
Finally, we will click on the checkboxes check_box1 and check_box2, that we found in the previous step using the click function.
click()
The function that is used to interact with clickable objects, such as buttons, checkboxes, etc. is known as the click function. It is always executed in the center of the element.
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 on the checkboxes check_box1.click(); check_box2.click(); } } |
Output:
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.