In this tutorial, I am going to discuss. how to handle HTML elements in selenium web driver. Here I am going to cover all the common elements like button, Input, radio button, Checkbox in selenium web driver. So let’s handle HTML elements in selenium web driver.
Handle Input in selenium web driver
There are some different types of input boxes provided by HTML. Like text, email, password, etc. But the functionality of all types of input is the same insert data and delete data.
To perform Automation operation with input box there is a simple two-step
Step 1: find the element by using one option from XPath, id, name, class or tag name, etc.
Step 2: perform operations like set data or clear data.
Set the text and clear the input field:
sendKeys("hello Selenium"); To clear text of input field. input_1.clear();
Handle Button in selenium web driver
To perform automation operation with button we just need click() method.
Follow the simple two-step:
- Find the element(Button)
- Click on the element(Button)
// find buttton WebElement button = driver.findElement(By.xpath("//*[@id=\"get-input\"]/button")); //click on button button.click();
How to get text of a button in selenium web driver.
getText() method is used to get text of any element in selenium. In this example, we are going to take text of the button.
String buttontext = button.getText(); System.out.println(buttontext);
Automate input and button fields in selenium web driver with java
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; publicclass Main { publicstaticvoid main(String[] args) { System.setProperty("webdriver.chrome.driver", "D:\\Automation\\selenium\\lib_files\\chrome\\chromedriver.exe"); // Open Browser WebDriver driver = new ChromeDriver(); // Open the URL in current window of the browser driver.get("http://www.seleniumeasy.com/test/basic-first-form-demo.html"); // Find input element by xPath. WebElement input_1 = driver.findElement(By.xpath("//*[@id=\"user-message\"]")); // Clear text input_1.clear(); // set Text to the element..using sendKeys(); input_1.sendKeys("hello Selenium"); // find buttton WebElement button = driver.findElement(By.xpath("//*[@id=\"get-input\"]/button")); // click on button button.click(); //Get the text of button String buttontext = button.getText(); System.out.println(buttontext); } }
Handle Radio Button in selenium web driver
We can select the radio button by click() method and it is the simplest way to access any element.
WebElement radioButton = driver.findElement(By.xpath("//*[@id=\"easycont\"]/div/div[2]/div[2]/div[2]/div[1]/label[1]")); radioButton.click();
But some time we need to check the button is already select or not then we need to use isSelected(); method.
Let see an example of Radio button automation with selenium web driver.
// find the list of all radio buttons List<WebElement>radioButton = driver.findElements(By.name("optradio")); // check radio button at the index 0 is select on not in the list. if(radioButton.get(0).isSelected()) { //if its select just print a message on the console System.out.println("Radio Button is selected"); }else { //else select the radio button radioButton.get(0).click(); }
Handle CheckBox in selenium web driver
We can check Checkbox by click method it’s a toggle operation we just need to do no/off.
//Locate the checkbox(element...........) WebElement checkBox = driver.findElement(By.xpath("//*[@id=\"easycont\"]/div/div[2]/div[1]/div[2]/div[1]/label")); //Click on the checkbox(element) checkBox.click();
If we need to check the checkbox is already select or not we can use isSelect(); similar as mention above.
driver.navigate().to("http://www.seleniumeasy.com/test/basic-checkbox-demo.html"); // Locate the check box(element...........) WebElement checkBox = driver .findElement(By.xpath("//*[@id=\"easycont\"]/div/div[2]/div[1]/div[2]/div[1]/label")); // check that is default select or not. if(checkBox.isSelected()) { System.out.println("Check box is already selected"); }else { // Click on the check box(element) checkBox.click(); }
How to Select multiple checkbox using selenium web driver.
There is not any direct method to select all checkbox So we need to perform some logic here.
Step 1: Locate all the checkboxes using a common attribute like class, name, etc.
Step 2: Store all the elements in a List.
Step 3: Get the elements by using a loop one by one and perform click operation.
driver.navigate().to("http://www.seleniumeasy.com/test/basic-checkbox-demo.html"); // Locate the check box(element...........) List<WebElement>checkBox = driver.findElements(By.className("cb1-element")); for(WebElement cblist : checkBox) { cblist.click(); }
Automate Radio button and Checkboxes fields in selenium web driver with java
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 Main { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "D:\\Automation\\selenium\\lib_files\\chrome\\chromedriver.exe"); // Open Browser WebDriver driver = new ChromeDriver(); // Open the URL in current window of the browser driver.get("http://www.seleniumeasy.com/test/basic-radiobutton-demo.html"); /************************ Radio Button ****************************************/ // find the list of all radio buttons List<WebElement> radioButton = driver.findElements(By.name("optradio")); // check radio button at the index 0 is select on not in the list. if (radioButton.get(0).isSelected()) { // if its select just print a message on the console System.out.println("Radio Button is selected"); } else { // else select the radio button radioButton.get(0).click(); } /******************************** * Check Box *************************************/ driver.navigate().to("http://www.seleniumeasy.com/test/basic-checkbox-demo.html"); // Locate the check box(element...........) List<WebElement> checkBox = driver.findElements(By.className("cb1-element")); for (WebElement cblist : checkBox) { cblist.click(); } } }