For BE/B.Tech/BCA/MCA/ME/M.Tech Major/Minor Project for CS/IT branch at minimum price Text Message @ 9424820157

How to get Text of Checkbox in Selenium

Get Text of Checkbox in Selenium

Below is the simple example step by step showing how to get text of checkbox in java by using selenium webdriver.

What is Checkbox?
A checkbox is a Graphical widget that permits the user to make a binary choice, i.e. a choice between one of two possible mutually exclusive options. For example, the user may have to answer 'yes' or 'no' on a simple yes/no question. Checkboxes are shown as ☐ when unchecked, or ☑ or ☒ when checked.

The website which we are going to automate for getting text of checkbox is: training.mulesoft.com/login

Below is the screenshot of the login page of training.mulesoft.com and there is a checkbox which is denoted by red arrow.

First we will check the checkbox and then we will display the text in the console that is 'Remember' by using getText() in selenium webdriver.
















Steps to create Java project for checkbox example:

1)  Open eclipse and go to file and make java project
2) Create package inside java project with name: com.checkBox
3) Create class inside com.checkBox with name: TextWithCheckBox
4) Copy whole code written below and paste in the class TextWithCheckBox.java
5) Add chrome driver at lib folder as shown in code below.
6) Run the java class
7) After execution of chrome driver, in console we will get output as: Text: Remember


In the below code we are going to use className as web locator to check the checkbox.  
  WebElement element = driver.findElement(By.className("checkbox"));


Here is the code:

package com.checkBox;

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 TextWithCheckBox {
	
	static String projectLocation  = System.getProperty("user.dir");
	public static void main(String[] args) throws InterruptedException {
		System.setProperty("webdriver.chrome.driver", projectLocation+"/lib/chromedriver.exe");
		WebDriver driver = new ChromeDriver();	

		driver.manage().window().maximize(); // maximises window
		driver.manage().deleteAllCookies(); //delete cookies
		
		// dynamic wait
		driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		
		driver.get("https://training.mulesoft.com/login");
		
		//handle checkbox
		WebElement element = driver.findElement(By.className("checkbox"));
		element.click();
		System.out.println("Text: "+element.getText());
		
		
		Thread.sleep(3000);
		driver.quit();
	}

}


Output: 
Text: Remember


Run the above code, from method element.getText() you will get text of the checkbox which is Remember.

Let's see another example where we are going again to get text of checkbox.

In this example, we are automating website: https://designsystem.digital.gov/components/checkbox/

Below is the screenshot of the webpage having different checkboxes. Here we are going to automate checkbox having text 'Booker T. Washington'.




First we will check the checkbox and then we will display the text in the console that is 'Booker T. Washington' by using getText() in selenium webdriver.

Steps to create Java project for checkbox example:

1)  Open eclipse and go to file and make java project
2) Create package inside java project with name: com.checkBox
3) Create class inside com.checkBox with name: TextWithCheckBox2
4) Copy whole code written below and paste in the class TextWithCheckBox2.java
5) Add chrome driver at lib folder as shown in code below.
6) Run the java class
7) After execution of chrome driver, in console we will get output as: Text: Booker T. Washington

In the below code we are going to use xpath as web locator to check the checkbox.  
WebElement element = driver.findElement(By.xpath("//label[@for='check-historical-washington']"));

Here is the code,


package com.checkBox;

import java.util.concurrent.TimeUnit;

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

public class TextWithChechBox2 {
	
	static String projectLocation  = System.getProperty("user.dir");
	public static void main(String[] args) throws InterruptedException {
		System.setProperty("webdriver.chrome.driver", projectLocation+"/lib/chromedriver.exe");
		WebDriver driver = new ChromeDriver();	

		driver.manage().window().maximize(); // maximises window
		driver.manage().deleteAllCookies(); //delete cookies
		
		// dynamic wait
		driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		
		driver.get("https://designsystem.digital.gov/components/checkbox/");
		
		JavascriptExecutor js = (JavascriptExecutor) driver;
		js.executeScript("window.scrollBy(0,500)", "");
		
		//handle checkbox
		WebElement element = driver.findElement(By.xpath("//label[@for='check-historical-washington']"));
		element.click();
		System.out.println("Text: "+element.getText());
		
		
		Thread.sleep(3000);
		driver.quit();
	}


}
Output: 
Text: Booker T. Washington


Run the above code, from method element.getText() you will get text of the checkbox which is Booker T. Washington.


No comments:

Post a Comment



Please go through below tutorials:


Mule 4 Tutorials

DEPLOY TO CLOUDHUB C4E CLIENT ID ENFORCEMENT CUSTOM POLICY RABBIT MQ INTEGRATION
XML TO JSON WEBSERVICE CONSUMER VM CONNECTOR VALIDATION UNTIL SUCCESSFUL
SUB FLOW SET & REMOVE VARIABLE TRANSACTION ID SCATTER GATHER ROUND ROBIN
CONSUME REST WEBSERVICE CRUD OPERATIONS PARSE TEMPLATE OBJECT TO JSON LOAD STATIC RESOURCE
JSON TO XML INVOKE IDEMPOTENT FILTER FOR EACH FLAT TO JSON
FIXWIDTH TO JSON FIRST SUCCESSFUL FILE OPERATIONS EXECUTE ERROR HANDLING
EMAIL FUNCTIONALITY DYNAMIC EVALUATE CUSTOM BUSINESS EVENT CSV TO JSON COPYBOOK TO JSON
CHOICE ASYNC

Widely used Connectors in Mule 3

CMIS JETTY VM CONNECTOR SALESFORCE POP3
JMS TCP/IP WEBSERVICE CONSUMER QUARTZ MONGO DB
FILE CONNECTOR DATABASE CONNECTOR


Widely used Scopes in Mule 3

SUB FLOW REQUEST REPLY PROCESSOR CHAIN FOR EACH CACHE
ASYNC TCP/IP COMPOSITE SOURCE POLL UNTIL SUCCESSFUL
TRANSACTIONAL FLOW

Widely used Components in Mule 3

EXPRESSION CXF SCRIPT RUBY PYTHON
JAVASCRIPT JAVA INVOKE CUSTOM BUSINESS EVENT GROOVY
ECHO LOGGER


Widely used Transformers in Mule 3

MONGO DB XSLT TRANSFORMER REFERENCE SCRIPT RUBY
PYTHON MESSAGE PROPERTIES JAVA TRANSFORMER GZIP COMPRESS/UNCOMPRESS GROOVY
EXPRESSION DOM TO XML STRING VALIDATION COMBINE COLLECTIONS BYTE ARRAY TO STRING
ATTACHMENT TRANSFORMER FILE TO STRING XML TO DOM APPEND STRING JAVASCRIPT
JSON TO JAVA COPYBOOK TO JSON MAP TO JSON JSON TO XML FLATFILE TO JSON
FIXWIDTH TO JSON CSV TO JSON


Widely used Filters in Mule 3

WILDCARD SCHEMA VALIDATION REGEX PAYLOAD OR
NOT MESSAGE PROPERTY MESSAGE IDEMPOTENT FILTER REFERNCE
EXPRESSION EXCEPTION CUSTOM AND


Exception Strategy in Mule 3

REFERENCE EXCEPTION STRATEGY CUSTOM EXCEPTION STRATEGY CHOICE EXCEPTION STRATEGY CATCH EXCEPTION STRATEGY GLOBAL EXCEPTION STRATEGY


Flow Control in Mule 3

CHOICE COLLECTION AGGREGATOR COLLECTION SPLITTER CUSTOM AGGREGATOR FIRST SUCCESSFUL
MESSAGE CHUNK AGGREGATOR MESSAGE CHUNK SPLITTER RESEQUENCER ROUND ROBIN SOAP ROUTER