Locate a radio button and select it

Radio button operation is easy to perform in selenium webdriver. The operations that you perform mostly in your test automaton is selection and de-selection of radio button.

Using Click() method in Selenium we can perform the action on the Radio button.

In your automation scenario, sometimes you may also required to get the status of the radio button (Selected or de-selected). With IsSelected statement, you can get to know that the radio button is selected or not.

Locate a radio button and select it

Example Code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo {

 public static void main(String[] args) {

  System.setProperty("webdriver.gecko.driver", "./MyProject/driver/geckodriver.exe");
  WebDriver driver = new FirefoxDriver();
  driver.get("https://www.facebook.com/");

  // Select Male checkbox on Facebook login page
  WebElement MaleRadioBtn = driver.findElement(By.id("u_0_i"));
  MaleRadioBtn.click();

  // With IsSelected statement, you can get to know that the radio button is
  // selected or not
  boolean MaleRadioBtnStatus = MaleRadioBtn.isSelected();
  System.out.println("Status of Male radio button : " + MaleRadioBtnStatus);
 }
}


<-- Previous || Next -->

No comments:

Post a Comment