Handle Radio Button, CheckBox, Inputs and button in selenium web driver

Handle Radio Button, CheckBox, Inputs and button in selenium web driver with java

In this tutorial, we are going to discuss How to Handle Radio Button, CheckBox, Inputs and button in selenium web driver with java.

Input Fields:

There are some different types of input box 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 the simple two-step

Step 1: find the element by using one option from XPath, id, name, class or tag name etc.

Step 2: perform the operation like set data or clear data.

To set the text to the input field:

sendKeys("hello Selenium");

To clear text of the input field.

input_1.clear();

Complete Source code with example.

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) {

    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\"]"));

    // set Text to the element..using sendKeys();
    input_1.sendKeys("hello Selenium");

    // Clear text
    input_1.clear();

  }
}

Button:

To perform automation operation with the button we just need click() method.

Follow 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 the text of a button in selenium web driver.

getText() method is used to get the text of any element in selenium. In this example, we are going to take the text of the button.

String buttontext = button.getText();
System.out.println(buttontext);

Handle Inputs and button with selenium web driver

Complete Source code:

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) {

    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);

  }
}

In the below examples, we are going to learn how to perform automation operation with radio button and checkbox using selenium web driver.

Radio Button:

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 we need to use isSelected(); method.

Let see an example of Radio button automation with selenium web driver.

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:

We can check Check box by click method it’s a toggle operation we just need to do no/off.

//Locate the check box(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 like 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();
    }

Select multiple checkboxes 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 checkbox using a common attribute like class, name etc.

Step 2: Store all the elements in a List.

Step 3: Get the elements by using 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 CheckBox with selenium web driver

Complete Source code to automate the Radio button and Checkboxes using selenium web driver.

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();
    }
  }
}

 

 

Complete Selenium Web driver tutorial

Add a Comment

Your email address will not be published. Required fields are marked *