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

1 Answer: Use ID For Selection

The document asks for help selecting a checkbox or radio button using Selenium WebDriver in a concise way. It provides an HTML sample with checkbox elements that have IDs. The response recommends using the findElement method to locate the element by ID and click it to select, and using the isSelected method to check the state of the element after selection.

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)
72 views

1 Answer: Use ID For Selection

The document asks for help selecting a checkbox or radio button using Selenium WebDriver in a concise way. It provides an HTML sample with checkbox elements that have IDs. The response recommends using the findElement method to locate the element by ID and click it to select, and using the isSelected method to check the state of the element after selection.

Uploaded by

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

sibletext for dropdown.

Going through the examples given for all other related questions I could not find a
proper solution that works in a concise way that by few line or method I can check a
checkbox or radio button.
Any help would be appreciated.
A sample HTML section is below:
<tbody> 
<tr> 
    <td> 
        <span class="120927"> 
        <input id="ctl00_CM_ctl01_chkOptions_0" type="checkbox"
name="ctl00$CM$ctl01$chkOptions$0"/> 
        <label
for="ctl00_CM_ctl01_chkOptions_0">housingmoves</label> 
        </span> 
    </td> 
</tr> 
<tr>
    <td>
        <span class="120928"> 
        <input id="ctl00_CM_ctl01_chkOptions_1" type="checkbox"
name="ctl00$CM$ctl01$chkOptions$1"/> 
        <label for="ctl00_CM_ctl01_chkOptions_1">Seaside &
Country Homes</label> 
        </span> 
    </td> 
</tr> 
</tbody> 
 selenium
 
 webdriver
 
 selenium-webdriver
 
 xpath

answer

1 Answer
+ –

0votes

answered Jul 11, 2019 by Prabhpreet Kaur (63.3k points)


Use ID For Selection:
You can use the ID attribute to select a Radio Button or a CheckBox. We’ve provided
the Webdriver command to click which you can apply to both types of elements.
Java code example to select checkbox/radio button.
 WebElement target = driver.findElement(By.id("checkbox1"));
 target.click();
Call IsSelected() To Check The State:
 If you’ve selected/deselected a Checkbox/Radio Button and you want to check its final
state. Then, you can use the <IsSelected> command to know the correct status of the
element.
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
 
public class findElementsTest {
 
   public static void main(String[] args) throws Exception {
 
      // Launch browser
      WebDriver driver = new FirefoxDriver();
 
      // Maximize window
      driver.manage().window().maximize();
 
      // Navigate to the URL
      driver.get("//www.techbeamers.com");
 
      // Sleep for 5 seconds
      Thread.sleep(5000);
 
      // Store all the elements of the same category in the
list of WebLements.
      List list = driver.findElements(By.name("radioButton"));
 
      // Create a boolean variable to store true/false.
      Boolean is_selected = list.get(0).isSelected();
 
      // If 'is_selected' is true that means the first radio
button is
      // selected.
      if (is_selected == true) {
 
         // If the first radio button is selected by default
then,
         // select the second radio button.
         list.get(1).click();
 
      } else {
 
         // If the first radio button is not selected then,
click the first
         // radio button.
         list.get(0).click();
      }
   }
}
For further assistance, please go for online Selenium tutorials. 

You might also like