Radio button using Selenium WebDriver

0 votes

I was implementing the radio button but I was not able to do it. Below is my HTML

<div class="appendContent">
    <div> id="contentContainer" class="grid_list_template">
        <div id="routeMonitorOptions"></div>
    </div>
    <input id="optionStopGrid" type="radio" name="gridSelector"/>
    <label for="optionStopGrid">Paradas</label>
</div>
Apr 11, 2018 in Selenium by shubham
• 6,890 points
1,193 views

2 answers to this question.

0 votes

This code will help you out:

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 Demo {
    public static void main(String[] args) throws InterruptedException {
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.google.com/");
    //If u want to know the number of radio buttons then use List
    List<WebElement>radioButton = driver.findElements(By.tagName("google"));
    System.out.println(radioButton.size());
    //If u want to select the radio button
    driver.findElement(By.id("google")).click();
    Thread.sleep(3000);
    //If u want the Text in U R console
    for(int i=0;i<radioButton.size();i++){
    System.out.println(radioButton.get(i).getText());
    } 
    //If u want to check whether the radio button is selected or not
    if(driver.findElement(By.id("example")).isSelected()){
        System.out.println("True");
    }else
    {
        System.out.println("False");
    }
  }
}

answered Apr 11, 2018 by Johnathon
• 9,080 points
0 votes

You can try following code snippet:

WebElement elem = driver.findElement(By.id("optionStopGrid"));
if(!elem.isSelected()){
        elem.click();
    }else
    {
        System.out.println("Radio button Selected!");
    }
answered Aug 28 by Abha
• 22,870 points

Related Questions In Selenium

+1 vote
1 answer
0 votes
1 answer
0 votes
3 answers

How to specify "ENTER" button functionality in Selenium WebDriver code?

using OpenQA.Selenium.Interactions; Actions builder = new Actions(driver); ...READ MORE

answered Feb 12 in Selenium by anonymous
8,782 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
555 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
888 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
551 views
0 votes
1 answer

Java.lang.CharSequence cannot be resolved error while using sendKeys() using Selenium Webdriver

You could try this, myElement .sendKeys(new String[] { ...READ MORE

answered Apr 4, 2018 in Selenium by Johnathon
• 9,080 points
649 views
+2 votes
4 answers

sendKeys() not working in Selenium Webdriver

this work for me you should update your ...READ MORE

answered Aug 27 in Selenium by Afif
12,218 views