Working with CheckBoxes and Radio Buttons
In the previous post, we have seen the working with WebList in selenium. In this post, we will see the working with CheckBoxes and Radio Buttons.
In our demo application, we have 3 checkboxes with color names Red,Green and White etc. We will see operations like select check box, deselect check box, choose radio button etc.
Uncheck the checkbox ‘Green’:
We will find a unique property of check box to make a XPATH. If we inspect check box using Firebug, then we observe that ‘Value’ attribute is having unique value among all check boxes. We will consider this to create the XPATH.
We will use the method click() to uncheck the checkbox.
public class CheckBoxExample { private static WebDriver driver; public static void main(String[] args) throws InterruptedException { driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("https://testingpool.com/wp-content/uploads/2015/08/DemoApplication.html"); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); WebElement CB_Green = driver.findElement(By.xpath("//input[@value='green']")); CB_Green.click(); } }
Check the checkbox for color ‘Red’:
The click() method will be used to check the checkbox.
public class CheckBoxExample { private static WebDriver driver; public static void main(String[] args) throws InterruptedException { driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("https://testingpool.com/wp-content/uploads/2015/08/DemoApplication.html"); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); WebElement CB_Red = driver.findElement(By.xpath("//input[@value='red']")); CB_Red.click(); } }
Selecting the radio button:
We have 2 radio buttons in application for ‘Male’ and ‘Female’. ‘Female’ radio button is by default selected. We need to select the radio button for ‘Male’.
We need to use method ‘Click()’ for selecting the radio button.
public class RadioBttnExample { private static WebDriver driver; public static void main(String[] args) throws InterruptedException { driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("https://testingpool.com/wp-content/uploads/2015/08/DemoApplication.html"); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); WebElement RB_Male = driver.findElement(By.xpath("//*[@id='male']")); //radio button RB_Male.click(); } }