Basic Selenium Tutorials

HOME

Selenium – Introduction, Installation, Test Script

Chapter 1 Introduction to Selenium Automation Tool
Chapter 2 How to Download & Install Java JDK 11 in Windows
Chapter 3 How to Download and Install Eclipse IDE
Chapter 4 How to install IntelliJ on Windows
Chapter 5 How to Download & Install Selenium WebDriver 
Chapter 6  How to create first Selenium WebDriver Script using Java
Chapter 7 How to run Selenium Tests using on Internet Explorer

Locators in Selenium

 Chapter 1 How to Locate Elements in Chrome, Firefox and IE Browsers for creating Selenium Scripts
Chapter 2 Locators in Selenium – Locate by ID, ClassName,  Name, TagName,  LinkText, PartialLinkText
Chapter 3 Dynamic XPath  in Selenium WebDriver
Chapter 4 CSS Selector in Selenium WebDriver

Launching Browsers and headless Browser

Chapter 1 How to run Chrome tests in headless mode in Selenium
Chapter 2 How to run Firefox tests in headless mode in Selenium
Chapter 3 How to run Edge tests in headless mode in Selenium4
Chapter 4 How to manage driver executables using WebDriverManager
Chapter 5 How to disable infobar warning for Chrome tests in Selenium
Chapter 6 How to maximize and minimize the window in Selenium

WebDriver Commands

Chapter 1 Difference between FindElement and FindElements in WebDriver
Chapter 2 Difference between getText() and getAttribute() method in WebDriver
Chapter 3 WebDriver Browser Commands – get,  getTitle, getCurrentUrl, getPageSource, getClass, close, quit in WebDriver
Chapter 4 WebDriver Navigation Commands – Navigate, Forward, Back, Refresh in  WebDriver
Chapter 5 Selenium Form WebElement Commands – Sendkeys, Clear, Click,Submit
Chapter 6 How to automate selecting Checkbox and Radio Buttons in Selenium WebDriver
Chapter 7 How to Select value from Drop down list or perform Multiple Selection  Operations in WebDriver
Chapter 8 How to get all options in a DropDown list in WebDriver
Chapter 9 How to automate Radio Button in WebDriver
Chapter 10 How to automate BootStrap DropDown using WebDriver
Chapter 15 How to handle Dynamic Web Tables using Selenium WebDriver
Chapter 16 How to get all the values from a Dynamic Table in Selenium WebDriver 
Chapter 17 isDisplayed, isSelected, isEnabled in Selenium
Chapter 18 How to test HTML ordered list in Selenium
Chapter 19 How to test HTML5 validation messages with Selenium

Waits in Selenium

Chapter 1 Implicit and Explicit Wait in Selenium WebDriver
Chapter 2 What is Fluent Wait in Selenium WebDriver

Handle Window and Alerts

Chapter 1 Switch Window Commands in Selenium WebDriver
Chapter 2 How to handle Alerts in Selenium WebDriver
Chapter 3 How to Switch Between Frames in Selenium WebDriver

Selenium Interview Questions and Answers
Advanced Selenium Interview Questions and Answers
Selenium Multiple Choice Questions – MCQ1
Selenium Multiple Choice Questions – MCQ1
Selenium Multiple Choice Questions – MCQ3

How to handle radio buttons in Robot Framework 

HOME

In this tutorial, we will automate the execution of the Radio Button in Robot Framework.

Prerequisite:

  1. Install Python
  2. Install PIP
  3. Install Robot Framework
  4. Install Robot framework Selenium Library
  5. Install PyCharm IDE

Please refer to this tutorial to install Robot Framework – How to install and setup Robot Framework for Python.

Implementation Steps:

Step 1.1 – Open PyCharm and create a new project. Go to File and select New Project from the main menu.

Step 1.2 – Choose the project location. Click the “Browse” button next to the Location field and specify the directory for your project.

Deselect the Create a main.py welcome script checkbox because you will create a new Python file for this tutorial.

Click on the “Create” Button.

Step 1.3 – A new dialog appears asking to open the project using any one of the given options. I have selected New Window as I like to have separate windows for each project.

Below is the image of the new project created in PyCharms.

Step 2 – Create a new directory in the new project

Right-Click on the project, select New->Directory and provide name as Tests

Below is the image of the new directory.

Step 3 – Create a robot Test File

Right-click on the new directory and select New File and provide the name as RadioButton_Demo.robot as shown below:

Step 4 – Download ChromeBinaries from the below location

The tests are going to use the Chrome browser, so we need to download the ChromeBinaries to open a blank browser in Chrome.

https://chromedriver.chromium.org/

The chromedriver and geckodriver are placed in a folder name drivers in the RobotFramework_Demo project. I have renamed chromedriver to Chrome and geckodriver to Firefox.

Step 5 – Automate the selection of options for Radio Buttons

We are now going to write test cases. The test case details will be as follows −

  • Open the browser and copy URL − https://demo.automationtesting.in/Register.html in Chrome
  • Verify that the page contains the Radio Buttons.
  • Verify that the page does not contain the Radio Button. Provided the locator for the checkbox
  • Click on “Male” Radio Button
  • Verify that “Male” Radio Button is selected

To work with the Radio Button, we need a locator. A locator is an identifier for the textbox like id, name, class, xpath, css selector, etc.

To know more about locators, refer to these Selenium Tutorials:

 Locators in Selenium – Locate by ID, ClassName,  Name, TagName,  LinkText, PartialLinkText

Dynamic XPath  in Selenium WebDriver

CSS Selector in Selenium WebDriver

The below page shows that we have 2 Radio Buttons – Male and FeMale.

Let us inspect the locator of the Radio Button.

Below is an example of selecting the “Male” option in Radio Buttons.

*** Settings ***
Documentation    To validate the Login Form
Library     SeleniumLibrary
Test Teardown    Close Browser

*** Test Cases ***
Select an option of Radio Button
    Open the Browser with URL
    Select impressive option from 3 radio buttons
    Verify that the radio button is selected


*** Keywords ***
Open the Browser with URL
    Create Webdriver    Chrome  executable_path=/Vibha_Personal/RobotFramework_Demo/drivers/chromedriver_linux64
    Go To    https://demo.automationtesting.in/Register.html
    Maximize Browser Window
    Set Selenium Implicit Wait    2


Select impressive option from 3 radio buttons
    Page Should Contain Radio Button    name:radiooptions
    Page Should Not Contain Radio Button    id:checkbox1    #This is checkbox
    Select Radio Button    radiooptions    Male


Verify that the radio button is selected
    Radio Button Should Be Set To    radiooptions    Male

All the below-mentioned keywords are derived from SeleniumLibrary. The functionality of keywords mentioned above:

1. Create Webdriver − The keyword creates an instance of Selenium WebDriver.

2. Go To – This keyword navigates the current browser window to the provided URL – https://demo.automationtesting.in/Register.html.

3. Maximize Browser Window – This keyword maximizes the current browser window.

4. Set Selenium Implicit Wait – This keyword sets the implicit wait value used by Selenium.

5. Page Should Contain Radio Button − This keyword is used to find the radio buttons that are located on the current page.

6. Page Should Not Contain Radio Button – This keyword is used to find that the radio buttons are not locator on the current page.

7. Select Radio Button – This keyword is used to click on the “Male” option of the radio buttons.

8. Radio Button Should Be Set To – This keyword verifies if the radio button “Male” is selected or not.

These keywords are present in SeleniumLibrary. To know more about these keywords, please refer to this document – https://robotframework.org/SeleniumLibrary/SeleniumLibrary.htm.

To run this script, go to the command line and go to directory tests.

Step 6 – Execute the tests

We need the below command to run the Robot Framework script.

robot RadioButton_Demo.robot

The output of the above program is

Step 7 – View Report and Log

We have the test case passed. The Robot Framework generates log.html, output.xml, and report.html by default.

Let us now see the report and log details.

Report

Right-click on report.html. Select Open In->Browser->Chrome(any browser of your wish).

The Report generated by the framework is shown below:

Log

Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.

Right-click on log.html. Select Open In->Browser->Chrome(any browser of your wish).

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

How to automate selecting Checkbox and Radio Buttons in Selenium WebDriver

 
 

In this tutorial, we will see how to identify the form elements like CheckBox and Radio Button

Toggling a CheckBox or Radio Button on/off is also done using the click() method.

IsSelected – IsSelected method, let us know that the element selected or not. Assume there are two Radio Buttons/Check Boxes, one selected by default and you want to select the other one for your test. Then, we use IsSelected method. When there is a group of Radio Buttons/Check Boxes on the page then, most probably, their names are same, but values are different. Then we use the Webdriver findElements command to get the list of web elements.

Below is the working example of CheckBox.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class Checkbox_Test {
     
      public static void main(String[] args) {
            System.setProperty("webdriver.chrome.driver","C:\\Users\\Vibha\\Desktop\\Drivers\\chromedriver_win32\\chromedriver.exe");

          WebDriver driver = new ChromeDriver();
          driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
          driver.get("https://www.seleniumeasy.com/test/basic-checkbox-demo.html");
 
           //Single option selection
           System.out.println("*******Single option selection *********");
           driver.findElement(By.id("isAgeSelected")).click();
           String Message = driver.findElement(By.id("txtAge")).getText();
           System.out.println("Message is :"+Message);    
 
       // close the web browser
        driver.close();   
    }
}

Output
*******Single option selection *********
Message is :Success - Check box is checked

How to click all options in the Checkbox

In the below image, there are 4 checkboxes. Initially, when checkboxes were not selected, the test mentioned on the button is – Check All and once all the options are checked, the text on button changes to – Uncheck All.

import java.util.List;
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 classCheckbox_Test {   
     
     public static voidmain(String[] args) throws Exception { 

 System.setProperty("webdriver.chrome.driver","C:\\Users\\Vibha\\Desktop\\Drivers\\chromedriver_win32\\chromedriver.exe");
          WebDriver driver = newChromeDriver();
          driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
          driver.get("https://www.seleniumeasy.com/test/basic-checkbox-demo.html");
 
          //Display the value of Button before clicking the options of checkbox
          String buttontext_beforeclick = driver.findElement(By.xpath(".//*[@id='check1']")).getAttribute("value");
          System.out.println("Text before click :"+buttontext_beforeclick);

          // Find the CheckBox by its classname
          List list = driver.findElements(By.xpath("//input[@type ='checkbox' and @class='cb1-element']") );            
             
          // Get the number of CheckBoxes available
          int CheckBox_Size = list.size();
          System.out.println("Number of Checkbox options :"+CheckBox_Size);             
            
          // Iterate the loop from first CheckBox to last Checkbox
          for(int i=0;i<CheckBox_Size;i++)
          {     
                list.get(i).click();               
           }
          String buttontext_afterclick = driver.findElement(By.xpath(".//*[@id='check1']")).getAttribute("value");
          System.out.println("Text after click :"+buttontext_afterclick); 
    driver.quit();      
     }
}

Output
Text before click :Check All
Number of Checkbox options :4
Text after click :Uncheck All

Similarly, below I have explained Radio Button.

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
 
 public class Radio_Test {

       public static void main(String[] args) {      
 
     System.setProperty("webdriver.gecko.driver","C:\\Users\\vibha\\Downloads\\geckodriver-v0.24.0-win64\\geckodriver.exe");

          FirefoxDriver driver = new FirefoxDriver();
          driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
          driver.get("https://www.seleniumeasy.com/test/basic-radiobutton-demo.html");

          List<WebElement> Radio_Options = driver.findElements(By.name("optradio"));    
  
          // Create a boolean variable which will hold the value (True/False)
          boolean radio_value = false;

          // This statement will return True, in case of first Radio button is selected
         radio_value = Radio_Options.get(0).isSelected();

         //Identify if Radio Button 1 is selected or not. If Button 1 is already selected, then select Button 2
         if(radio_value==true)
         {
              Radio_Options.get(1).click();
              System.out.println("Button Selected is :"+ Radio_Options.get(1).getAttribute("value"));  
         }
         else
         {
               Radio_Options.get(0).click();
               System.out.println("Button Selected is :"+ Radio_Options.get(0).getAttribute("value"));  
          }

          driver.findElement(By.id("buttoncheck")).click();
          String Button_Selected = driver.findElement(By.xpath("//*[@id='easycont']/div/div[2]/div[1]/div[2]/p[3]")).getText();
          System.out.println("Get Checked Value is :"+ Button_Selected); 
       
         //Group Radio Button Selection
         driver.findElement(By.xpath(".//input[@name='gender'and @value='Female']")).click();
         driver.findElement(By.xpath(".//input[@name='ageGroup' and @value='15 - 50']")).click();
         driver.findElement(By.xpath(".//*[@id='easycont']/div/div[2]/div[2]/div[2]/button")).click();
          String Group_Radio_Message = driver.findElement(By.xpath("//*[@id='easycont']/div/div[2]/div[2]/div[2]/p[2]")).getText();

          System.out.println("Get Values are :"+Group_Radio_Message);

         // close the web browser
        driver.close();
      }
}
 
Output
Button Selected is :Male
Get Checked Value is :Radio button 'Male' is checked
Get Values are :Sex : Female
Age group: 15 - 50