top of page

Selenium Java Tutorial

Handling Radio Buttons And CheckBoxes

What are RadioButtons ?

Radio button is simply a graphical webelement that controls the selection of options that means only one radio button can be selected from a set of radio buttons at a time.
Using selenium webdriver radio buttons can be controlled.

Handling RadioButtons In Selenium
To select a radio button firstly locate that webelement and then perform click() .

WebElement MicrosoftExcel=driver.findElement(By.xpath("//input[@value='Excel']"));

MicrosoftExcel.click();


To confirm the selection we can use isSelected() method that returns boolean and returns True if condition is fulfilled.

MicrosoftExcel.isSelected()

* To deselect the selected radio button just, select the another one.
Here is a comlpete working code for radiobuttons =>

package seleniumexamples;

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

public class RadioButtonExample {

public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub WebDriver driver; String baseURL="https://www.keynotesupport.com/internet/web-contact-form-example-radio-buttons.shtml"; //Launch the web browser System.setProperty("webdriver.chrome.driver","..\\SeleniumJava\\drivers\\chromedriver99.exe"); driver=new ChromeDriver(); //To maximize the size of window driver.manage().window().maximize(); //Navigate through an URL driver.get(baseURL); //Locate and select the radioButton WebElement MicrosoftExcel=driver.findElement(By.xpath("//input[@value='Excel']")); MicrosoftExcel.click(); //To change the selection... if(MicrosoftExcel.isSelected()) { Thread.sleep(3000); //Select another radioButton and deselect the first one.... driver.findElement(By.xpath("//input[@value='Photoshop']")).click(); Thread.sleep(3000); System.out.println("\n* 'Adobe Photoshop' Is Selected and 'Microsoft Excel' Is Deselected !!"); } driver.quit();

}

}

Output :

* 'Adobe Photoshop' Is Selected and 'Microsoft Excel' Is Deselected !!




Explain CheckBoxes.

It is also a graphical webelement but the difference is that multiple checkboxes can be selected from the set of checkboxes at a time.
Handling CheckBoxes In Selenium
Locate the graphical checkbox webelement and perform click() .

WebElement ShowPassword=driver.findElement(By.className("VfPpkd-muHVFf-bMcfAe"));

ShowPassword.click();

Again, isSelected() method can be used for confirmation.

ShowPassword.isSelected()

* To uncheck the checked box click again on it.
Here is a comlpete working code for checkBox =>

package seleniumexamples;

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

public class CheckBoxExample {

public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub WebDriver driver; String baseURL="https://accounts.google.com/signup/v2/webcreateaccount?flowName=GlifWebSignIn&flowEntry=SignUp"; //Launch the web browser System.setProperty("webdriver.chrome.driver","..\\SeleniumJava\\drivers\\chromedriver99.exe"); driver=new ChromeDriver(); //To maximize the size of window driver.manage().window().maximize(); //Navigate through an URL driver.get(baseURL); //Locate and Check the CheckBox WebElement ShowPassword=driver.findElement(By.className("VfPpkd-muHVFf-bMcfAe")); ShowPassword.click(); //To verify Checkbox is Checked if(ShowPassword.isSelected()) { Thread.sleep(3000); //Uncheck the CheckBox System.out.println("\nCheckBox Is Checked Successfully !!"); ShowPassword.click(); } else System.out.println("\nFAIL! CheckBox Is Not Checked !!"); driver.quit(); }

}


Output :

*CheckBox Is Checked Successfully !!

Refer next page Scrolling A Web Page
bottom of page