简体   繁体   中英

How to check a radio button with Selenium WebDriver?

I want to check this radio button, but I didn't know how. My HTML is:

<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>

请尝试使用此代码选择单选按钮-

driver.findElement(By.cssSelector("input[id='optionStopGrid']")).click();
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 answer {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.example.com/");
        //If u want to know the number of radio buttons then use List
        List<WebElement>radioButton = driver.findElements(By.tagName("example"));
        System.out.println(radioButton.size());
        //If u want to select the radio button
        driver.findElement(By.id("example")).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");
        }
    }
}

This question should definitely help you.

You can find the element easily by id, then you just have to call the isSelected method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM