All IT Courses 50% Off
Selenium Tutorials

Selenium WebDriver CheckBox and Radio Button

Radio Button

Using the click() method a radio button can be accessed by clicking on it.

Using https://www.facebook.com/, see that radiobutton1.click() toggles on the “Female” radio button. Radiobutton2.click() toggles on the “Male” radio button leaving the “Female” unselected.

WebElement femaleradiobutton1 = driver.findElement(By.id(“u_0_6”));

WebElement maleradiobutton2 = driver.findElement(By.id(“u_0_7”));

//Female Radio Button1 is selected

All IT Courses 50% Off

Selenium WebDriver CheckBox and Radio Button

//Female Radio Button1 is de-selected and Radio Button2 is selected

Selenium WebDriver CheckBox and Radio Button

Let’s create a test case in which we will automate the following scenarios to handle Radio buttons:

  • Invoke a Google chrome browser.
  • Navigate to the website in which you handle the Radio button.
  • Radio Button1 is selected (Female) from the facebook website.
  • Radio Button1(Female) is de-selected and Radio Button2(Male) is selected
  • Close the driver.

Complete Code: 

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

public class Radiobutton{

    public static void main(String[] args) {

     // set the system property of a chromedriver.exe file to the path.

        System.setProperty("webdriver.chrome.driver"," D:\\Drivers\\geckodriver.exe ");

// Launch the Google Chrome browser.

        WebDriver driver = new ChromeDriver();

        driver.get("https://www.facebook.com/");

        WebElement femaleradiobutton1 = driver.findElement(By.id(“u_0_6”));

        WebElement maleradiobutton2 = driver.findElement(By.id(“u_0_7”));
       

        //Radio Button1 is selected

        femaleradiobutton1.click();

        System.out.println("Female Radio Button is Selected");

        //Female Radio Button1 is de-selected and Male Radio Button2 is selected

        maleradiobutton2.click();

        System.out.println("Male Radio Button is Selected");

          }

driver.close();

}

Check Box

Checking and Un-Checking a check box on/off is done using the click() method.

The below code will click on Yahoo “Stay signed in” check box two times and display the output as TRUE when it is clicked on, and FALSE if it is Unchecked.

 

Step 1: Go to the site https://login.yahoo.com/config/login?

Step 2: Click on Sign in button

Selenium WebDriver CheckBox and Radio Button

Step 3: Right Click on Stay signed in and select Inspect

Selenium WebDriver CheckBox and Radio Button

Step 4: Inspect the element Stay signed in

Selenium WebDriver CheckBox and Radio Button

 

Selenium WebDriver CheckBox and Radio Button

First Click– checkbox was clickedSelenium WebDriver CheckBox and Radio Button

Second click – checkbox was unchecked

The isSelected() method is used to know whether the Checkbox is selected on or off.

Let’s create a test case in which we will automate the following scenarios to handle Checkbox:

  • Invoke a Google chrome browser.
  • Navigate to the website in which you handle the checkbox.
  • Select the ‘Armed Forces/ Military ID card/ Dependent ID card’ checkbox from the spicejet website.
  • Close the driver.

Now, we will create a test case step by step in order to understand of how to handle checkbox.

Step 1: Launch the Eclipse IDE

Step 2: Right click on the src folder and click on the New > class.

Step 3: Enter the Class Name as (Checkbox)

Step 4: Invoke the Google Chrome browser and set the system property to the path of your chromedriver.exe file.

Step 5: Navigate to the “spicejet” website.

Here is the complete code for above scenario:

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

public class Checkbox {  
 
 public static void main(String[] args) {  
 // TODO Auto-generated method stub  
 System.setProperty("webdriver.chrome.driver",  D:\\Drivers\\geckodriver.exe ");  
 WebDriver driver = new ChromeDriver();  
 driver.get("https://www.spicejet.com/");  
 }

}  

Step 6: Now we try to locate the “Armed Forces/ Military ID card/ Dependent ID card” checkbox by inspecting its HTML code.

Selenium WebDriver CheckBox and Radio Button

Note the id attribute of a “Armed Forces/ Military ID card/ Dependent ID card” checkbox.

Selenium WebDriver CheckBox and Radio Button

Step 7: Write the code to handle the “Forces/ Military ID card/ Dependent ID card” checkbox.

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

public class Checkbox
{  
 public static void main(String[] args)   
{  
        // TODO Auto-generated method stub  
  System.setProperty("webdriver.chrome.driver", D:\\Drivers\\geckodriver.exe ");  

        WebDriver driver = new ChromeDriver();  

        driver.get("https://www.spicejet.com/");  

        System.out.println(driver.findElement(By.id("ctl00_mainContent_chk_IndArm")).isSelected());  

driver.findElement(By.id("ctl00_mainContent_chk_IndArm")).click();  

        System.out.println(driver.findElement(By.id("ctl00_mainContent_chk_IndArm")).isSelected());   

driver.close();  

    }    

} 

We have used two methods in the above code:

  • isSelected(): This method defines whether the checkbox is selected or not. If the checkbox is selected, then this method will return true otherwise false.
  • click(): This method selects the locator. In this case, it is selecting the “Forces/ Military ID card/ Dependent ID card ” checkbox.

 

 

 

Facebook Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button