Finding an Element
Once the website is loaded, we will now find an element, i.e., checkboxes, that needs to be handled using the findElement, By.xpath and contains function.
- findElement: The function used to find the first occurrence of the element in the webpage is known as the findElement function.
- By.xpath: The function that is used to locate elements by the relative path is known as the By.xpath function.
- contains(): The way of finding an element based on a partial text in the webpage is known as the contains function.
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')]" )); } } |
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.