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
• 7,340 points
4,745 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,090 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, 2019 by Abha
• 28,140 points

Related Questions In Selenium

+1 vote
1 answer
0 votes
1 answer

How to automate radio button and checkbox on a webpage in Selenium Webdriver?

Hey Yashmita, following code snippet automates the ...READ MORE

answered Jul 23, 2019 in Selenium by Anvi
• 14,150 points
2,347 views
0 votes
2 answers

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,500 points
13,629 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,119 views
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,910 points
7,207 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,859 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,090 points
4,773 views
+2 votes
4 answers

sendKeys() not working in Selenium Webdriver

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

answered Aug 27, 2019 in Selenium by Afif
30,137 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP