Handle Check Box , Radio Button , Text Box and Links in selenium

Check box , Text Box , Radio buttons and Links are all part of forms . In this Selenium online Tutorial we will learn how to handle check box, radio button , text box , button and links in selenium. These all are the most important part of any web application.

Working with Check box

Check box are used in the web page forms which has two options “Check” or “UnCheck“. During the registration forms we have seen so many check box like the most common is “Accepting terms and condition ” .


How to check/Unchecked the check box in selenium?

We can check the check box using click() method and to un-check the check box we can click on checked checkbox.


Now in the below demo image we have three check box of Hobbies – “Singing” , “Dancing” and “Acting“. and we will select the two check box “Singing” and “Acting ” here.

Here In this below program we first click on the check box “Singing”  and after that we will check the whether the “Acting” check box is selected or not . If it is not selected then check the checkbox only. Here we will check the check box selection with .ischecked() method which returns boolean value.

Before Program Execution

working-check-box-selenium1


After Program Execution

working-check-box-seleniumJava Program Handling Check box in selenium

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Checkbox_Tutorial {

	public static void main(String[] args) {

		// setting the driver driver path				
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
		WebDriver driver=new ChromeDriver();
		//openbrowser
		driver.manage().window().maximize(); // maximize the window
		// implicit wait is defined with max waiting time of 10 seconds
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		//open url
		//find the web element link 1 - Training
		driver.get("https://padhle.com/automation-practice-demo/"); // Open URL
		//find check box "Singing"
		WebElement Checkbox_Singing = driver.findElement(By.name("hobby1"));
		// click on the checkbox - select the checkbox
		Checkbox_Singing.click();
		//find check box "Acting"
		WebElement Checkbox_Acting = driver.findElement(By.name("hobby3"));
		// Check the checkbox Acting is selected or not
		if(Checkbox_Acting.isSelected()==false){
                System.out.println("Check box is uncheked");
		// click on the checkbox - select the checkbox
		Checkbox_Acting.click();
		}

	}

}

Working with Radio button in Selenium

Radio buttons in selenium is also similar like check box but in radio buttons you can choose only one option among them like we have to select our “Gender” in some form and we have three options only “Male” or “Female” or “Others”.
Radio button can be handled by click() method only. We can click on the radio button which we want to select.

Here In this Program we will find the element radio Button “Male” and click on that.

Before Program Execution
Radio-Button-Selenium
After Program Execution – Clicked on “Male” Radio Button and it is selected now.

Radio-Button-SeleniumJava Program – Handle Radio button in selenium

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class RadioButton_Tutorial {

	public static void main(String[] args) {

		// setting the driver driver path				
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");		
		WebDriver driver=new ChromeDriver();//openbrowser
		driver.manage().window().maximize(); // maximize the window
		// implicit wait is defined with max waiting time of 10 seconds
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.get("https://padhle.com/automation-practice-demo/");//open url
		//find radion button with Xpath
		WebElement RadioButton_Male = driver.findElement(By.xpath("//input[@value='male']"));
		// click on the Radio Button - select the radio button
		RadioButton_Male.click();	

	}

}


Working with Text Box in Selenium

Text box are the essential part of any application and we insert the text values in the text box. Sendkeys(“TexttoSend”) is used to send the values in the text box.

Here in the below program we will send the keys to the Text box “Selenium Tutorial” and after that we will clear the TextBox values using clear() method.

Methods of Text Box

  1. SendKeys(TexttoSend) – It send the text to text box
  2. Clear() – It will clear the text box value or empty the text box.

Java program to send values to text box in selenium

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class TextBox_Tutorial {

	public static void main(String[] args) {
		// setting the driver driver path				
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");		
		WebDriver driver=new ChromeDriver();//openbrowser
		driver.manage().window().maximize(); // maximize the window
		// implicit wait is defined with max waiting time of 10 seconds
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.get("https://padhle.com/automation-practice-demo/");//open url
		//find Text box 
		WebElement TextBox = driver.findElement(By.xpath("//input[@value='male']"));
		// Send "Selenium Tutorial to the text Box
		TextBox.sendKeys("Selenium Tutorial");	
		// Clear the text box value
		TextBox.clear();
	}
}

Keys are sent to Text BoxText-box-selenium


Working with Links in Selenium

Links in selenium are handled with click() method. Link are used to navigate to other website or different web pages within the website.

Here in this below program we will fetch the link first with By.linktext and after that we will click on the link with click() method.

Java Program to click on the link in selenium web driver

//

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Link_Tutorial {

	public static void main(String[] args) {
		// setting the driver driver path				
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
		WebDriver driver=new ChromeDriver();
		//openbrowser
		driver.manage().window().maximize(); // maximize the window
		// implicit wait is defined with max waiting time of 10 seconds
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		//open url and navigate to website		
		driver.get("https://padhle.com/automation-practice-demo/"); // Open URL
		//find Link 
		WebElement Link_Element = driver.findElement(By.linkText("Visit our Website"));
		// click on the Link
		Link_Element.click();
		
	}

}

Above program will click on the below link.

links in selenium


Working with Buttons in Selenium

Buttons in the web page generates some event like while we submit the login form it will navigate to some other page. To handle the buttons we have the click() method in selenium.

Java program to click on button using click() method in selenium

package convert.Maven_Basics;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Button_Tutorials {

	public static void main(String[] args) {
	
		// setting the driver driver path				
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
		WebDriver driver=new ChromeDriver();
		//openbrowser
		driver.manage().window().maximize(); // maximize the window
		// implicit wait is defined with max waiting time of 10 seconds
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		//open url and navigate to website		
		driver.get("https://www.facebook.com/"); // Open URL
		//find button element 
		WebElement Button_Element = driver.findElement(By.name("websubmit"));
		// click on the "Create Account" button
		Button_Element.click();

	}

}

Before clicking the “Create Account” button

Handling Buttons-in-selenium

After clicking the “create Account” button – Mandatory Fields are highlighted in red

Handling Buttons-in-seleniumNote : we can also call submit() method to submit any form like “Create Account” form.Submit() method can be called from any form element.

Hope this tutorial clearly explains about the handling of web elements in selenium like button , checkbox , radio button and etc. If you have any doubts regarding this just drop an email or post your question in below comments and can also inquire for the selenium online training with interview preparation.


Enroll for the Selenium online Training

Leave a Comment

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