0% found this document useful (0 votes)
98 views

Different Selection Method by Id: Boxes and Radio Buttons. We Might Like To Check That If The Checkbox Is

CheckBox and Radio Button operations can be performed using different selection methods like by ID, isSelected, value, and CSS selector. The document provides examples of using each method to select checkboxes and radio buttons including challenges to select specific options on a practice form using each method. The solution code provided selects the options as specified in the challenges by finding elements, checking selection status, and clicking using the appropriate method.

Uploaded by

Kavitha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Different Selection Method by Id: Boxes and Radio Buttons. We Might Like To Check That If The Checkbox Is

CheckBox and Radio Button operations can be performed using different selection methods like by ID, isSelected, value, and CSS selector. The document provides examples of using each method to select checkboxes and radio buttons including challenges to select specific options on a practice form using each method. The solution code provided selects the options as specified in the challenges by finding elements, checking selection status, and clicking using the appropriate method.

Uploaded by

Kavitha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CheckBox & Radio Button Operations are easy to perform and most of

the time the simple ID attributes work fine for both of these.


But selection and d-selection is not the only thing we want with Check
Boxes and Radio Buttons. We might like to check that if the CheckBox is
already checked or if the Radio Button is selected by default or
anything. Check Boxes and Radio Button deals exactly the same way and
you can perform below-mentioned operations on either of them.
 

Different Selection Method


 

By ID
If ID  is given for the Radio Button/CheckBox and you just want to click on it
irrespective of its value, then the command will be like this:

1 WebElement radioBtn = driver.findElement(By.id("toolsqa"));



3 radioBtn.click();
 

With IsSelected
If your choice is based on the pre-selection of the Radio
Button/CheckBox and you just need to select the deselected Radio
Button/CheckBox. Assume there are two Radio Buttons/Check Boxes, one is
selected by default and you want to select the other one for your test.
With IsSelected statement, you can get to know that the element is
selected or not.

1 // Store all the elements of same category in the list of WebLements


2  
3 List  oRadioButton = driver.findElements(By.name("toolsqa"));
4  
5 // Create a boolean variable which will hold the value (True/False)
6  
7 boolean bValue = false;
8  
9 // This statement will return True, in case of first Radio button is selected
10  
11 bValue = oRadioButton.get(0).isSelected();
12  
13 // This will check that if the bValue is True means if the first radio button is selected
14  
15 if(bValue = true){
16  
17 // This will select Second radio button, if the first radio button is selected by default
18  
19 oRadioButton.get(1).click();
20  
21 }else{
22  
23 // If the first radio button is not selected by default, the first will be selected
24  
25 oRadioButton.get(0).click();
26  
27 }
Note: Name is always same for the same group of Radio Buttons/Check
Boxes but their Values are different. So if you find the element with the
name attribute then it means that it may contain more than one element,
hence we need to use findElements method and store
the list of WebElements.
 

With Value
You can even select Radio Buttons/Check Boxes with their Values.

1 // Find the checkbox or radio button element by Name


2  
3 List oCheckBox = driver.findElements(By.name("tool"));
4  
5 // This will tell you the number of checkboxes are present
6  
7 int iSize = oCheckBox.size();
8  
9 // Start the loop from first checkbox to last checkboxe
10  
11 for(int i=0; i < iSize ; i++ ){
12  
13 // Store the checkbox name to the string variable, using 'Value' attribute
14  
15 String sValue = oCheckBox.get(i).getAttribute("value");
16  
17 // Select the checkbox it the value of the checkbox is same what you are looking for
18  
19 if (sValue.equalsIgnoreCase("toolsqa")){
20  
21 oCheckBox.get(i).click();
22  
23 // This will take the execution out of for loop
24  
25 break;
26  
27 }
28  
29 }
 

By CssSelector
A simple way of selecting a check-box or radio button is by using its value:

1 WebElement oCheckBox = driver.findElement(By.cssSelector("input[value='Tools QA']"));



3 oCheckBox.click();
 

Practice Exercise
1. Launch new Browser
2. Open “http://toolsqa.com/automation-practice-form/“
3. Challenge One – Select the deselected Radio button (female) for
category Sex (Use IsSelected method)
4. Challenge Two – Select the Third radio button for category ‘Years of
Exp’ (Use Id attribute to select Radio button)
5. Challenge Three – Check the CheckBox ‘Automation Tester’ for
category ‘Profession'( Use Value attribute to match the selection)
6. Challenge Four – Check the CheckBox ‘Selenium IDE’ for category
‘Automation Tool’ (Use cssSelector)
Solution

1 package automationFramework;
2  
3 import java.util.List;
4 import java.util.concurrent.TimeUnit;
5  
6 import org.openqa.selenium.By;
7 import org.openqa.selenium.WebDriver;
8 import org.openqa.selenium.WebElement;
9 import org.openqa.selenium.firefox.FirefoxDriver;
10  
11 public class CheckBxRadioBtn {
12  
13 public static void main(String[] args) {
14 // Create a new instance of the FireFox driver
15 WebDriver driver = new FirefoxDriver();
16
17 // Put an Implicit wait,
18 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
19
20
21 // Launch the URL
22 driver.get("http://toolsqa.com/automation-practice-form");
23
24 // Step 3 : Select the deselected Radio button (female) for category Sex (Use IsSelected method)
25 // Storing all the elements under category 'Sex' in the list of WebLements
26 List<WebElement> rdBtn_Sex = driver.findElements(By.name("sex"));
27
28 // Create a boolean variable which will hold the value (True/False)
29 boolean bValue = false;
30
31
// This statement will return True, in case of first Radio button is selected
32
bValue = rdBtn_Sex.get(0).isSelected();
33
34
35 // This will check that if the bValue is True means if the first radio button is selected
36 if(bValue == true){
37 // This will select Second radio button, if the first radio button is selected by default
38 rdBtn_Sex.get(1).click();
39 }else{
40 // If the first radio button is not selected by default, the first will be selected
41 rdBtn_Sex.get(0).click();
42 }
43
44 //Step 4: Select the Third radio button for category 'Years of Exp' (Use Id attribute to select Radio button)
45 WebElement rdBtn_Exp = driver.findElement(By.id("exp-2"));
46 rdBtn_Exp.click();
47
48 // STep 5: Check the Check Box 'Automation Tester' for category 'Profession'( Use Value attribute to match the selection)
49 // Find the Check Box or radio button element by Name
50 List<WebElement> chkBx_Profession = driver.findElements(By.name("profession"));
51
52
53 // This will tell you the number of Check Boxes are present
54 int iSize = chkBx_Profession.size();
55
56 // Start the loop from first Check Box to last Check Boxe
57 for(int i=0; i < iSize ; i++ ){
58 // Store the Check Box name to the string variable, using 'Value' attribute
59 String sValue = chkBx_Profession.get(i).getAttribute("value");
60
61 // Select the Check Box it the value of the Check Box is same what you are looking for
62 if (sValue.equalsIgnoreCase("Automation Tester")){
63 chkBx_Profession.get(i).click();
64 // This will take the execution out of for loop
65 break;
66 }
}
// Step 6: Check the Check Box 'Selenium IDE' for category 'Automation Tool' (Use cssSelector)
67 WebElement oCheck Box = driver.findElement(By.cssSelector("input[value='Selenium IDE']"));
68 oCheck Box.click();
69
70
71 // Kill the browser
72 driver.quit();
73  
74 }
 
}
 

 
 
 

Category: Selenium-Webdriver

You might also like