How To Handle Radio Button in Selenium Using Java

.
         Handling Radio Button in Selenium Webdriver 


In this tutorial, we are going to learn how to automate the radio button in the Selenium web driver.

We will Learn :
  • How to Select Radion Button.
  • How to Verify Whether the radio button is selected successfully or not.
  • How to Verify Whether the Radio Button is selected by default.
  • How to Select other radio buttons if the first radio button is selected default.
  • How to Verify Whether the Radio Button is Available or not.
  • How to Verify Whether the Radion button is enabled or not

How To Select Radio Button in Selenium Webdriver Using Java?

Click() 
By using the click method we select a radio button in Selenium webdriver.

Selenium Code 

// To select Radio button
driver.findElement(By.xpath("//label[@for='yesRadio']")).click();
System.out.println("Radio Button selected successfully ");


How To Verify Radio Button is Selected by default in Selenium using Java?

isSelected()
In the Selenium web driver to check whether the radio button is selected or not we use the isSelected () method.

Selenium Code 

//How to check radio button is selected or not by default
WebElement Wb=driver.findElement(By.xpath("//label[@for='yesRadio']"));
boolean ar=Wb.isSelected();
System.out.println(ar);
if(ar==false){
Wb.click();}
System.out.println("Radio Button selected successfully ");


How To Verify Whether Radio Button Is Enabled or Not in Selenium using Java?

isEnable() 
To verify whether a radio button is in disable mode or enable mode we use a predefined method known as isEnable method

Selenium Code
//how to check radio button is enabled in selenium
WebElement Wb=driver.findElement(By.id("noRadio"));
boolean rs =Wb.isEnabled();
//System.out.println("Radio button is not enable");
if(rs==true)
{
Wb.click();
}


How To Verify Whether The Radio Button is Displayed or Not on a Webpage in Selenium Webdriver?

isDisplayed ()
To verify whether a radio button is displayed on the webpage or not we use the predefined method isDisplayed.

Selenium Code 

/how to verify radio button is displayed on the webpage or not
WebElement Wb=driver.findElement(By.xpath("//label[@for='yesRadio']"));
        boolean ar=Wb.isDisplayed();
        if(ar==true) {
         Wb.click();
        }


How To Select Other Radio Button if the First Radio button is Selected by default in Selenium using Java?

isSelected()
In the Selenium web driver to check whether the radio button is selected or not we use the isSelected () method.

Selenium Code 

//How to select other radio if first is selected by default
WebElement Wb=driver.findElement(By.xpath("//label[@for='yesRadio']"));
boolean ar=Wb.isSelected();
System.out.println(ar);
if(ar==true){
driver.findElement(By.xpath("//label[@for='yesRadio']")).click();}
System.out.println(" Other Radio Button selected successfully ");

Hope!!! The above tutorial on handling the radio button in Selenium using Java is helpful for you...

Team, 
QA acharya 

How to select Radio button in Selenium 


Post a Comment

0 Comments