Selecting Radio Button in Selenium

Profile picture for user arilio666

To select a radio button in Selenium, you can use the WebDriver method .click() on the chosen radio button element. You first need to locate the radio button element using the .find_element_by_* method, where * is the element locating strategy, such as id, name, class name, etc.

  • Consider this site. We will use the radio button here to select them.
  • Let us choose one of the options using the click method.
  • From the DOM, we can take the xpath out using the value.
//input[@value='CA'] for canada.
//input[@value='US'] for United States.
//input[@value='IN'] for India.
        WebDriverManager.chromedriver().setup();
        ChromeDriver driver = new ChromeDriver();
        driver.get("http://autopract.com/selenium/form5/");
        driver.manage().window().maximize();
        driver.findElement(By.xpath("//input[@value='CA']")).click();
        driver.findElement(By.xpath("//input[@value='mac']")).click();
  • This is how we can write locators to select radio buttons using selenium webdriver.