如何使用Selenium WebDriver检查单选按钮? [英] How to check a radio button with Selenium WebDriver?

查看:69
本文介绍了如何使用Selenium WebDriver检查单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查此单选按钮,但我不知道如何.我的HTML是:

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>

推荐答案

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");
        }
    }
}

这篇关于如何使用Selenium WebDriver检查单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆