Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

selenium
java

How to select checkboxes and radio buttons in Selenium WebDriver

Ravi

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

Selenium is a powerful tool for programmatically manipulating an internet browser. All major browsers can use it. It runs on all major operating systems, and it has scripts written in many languages, including Python, Java, C#, and others.

Add the following dependency to pom.xml:

<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.3.0</version>
</dependency>

The click() method

The click() method can be used for toggling a check box and on/off the radio buttons.

Syntax

WebElement.click()

Example

public class Main{
public static void checkUncheckBox(WebElement webElement){
// check the check box
webElement.click();
// uncheck the check box
webElement.click();
}
public static void toggleRadioButtons(WebElement webElement){
// turn on the radio button
webElement.click();
// turn off the radio button
webElement.click();
}
public static void main (String[] args) {
System.setProperty("webdriver.gecko.driver","path to driver");
String url = "https://educative.io/";
WebDriver driver = new FirefoxDriver();
driver.get(url);
String tagName = "checkBox";
WebElement checkBox = driver.findElement(By.tagName(tagName));
checkUncheckBox(checkBox);
tagName = "radioButton";
WebElement radioButton = driver.findElement(By.tagName(tagName));
toggleRadioButtons(radioButton);
driver.close();
}
}
Example

Explanation

  • Line 19: We set the system property, webdriver.gecko.driver, to point to the firefox driver.
  • Line 20: We define the URL to be opened.
  • Line 21: We define an instance of the FirefoxDriver.
  • Line 22: We open the URL using the get() method.
  • Line 23: We define the tag name for selecting the check box HTML element.
  • Line 24: We retrieve the web element using the tag name tagName.
  • Line 25: We invoke the checkUncheckBox() method where the check box HTML element is checked and unchecked.
  • Line 27: We define the tag name for selecting the radio button HTML element.
  • Line 28: We retrieve the web element using the tag name tagName.
  • Line 29: We invoke the toggleRadioButtons() method where the radio button HTML element is turned on and off.
  • Line 31: We close the driver.

RELATED TAGS

selenium
java

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring