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/
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