简体   繁体   English

如何使用Selenium WebDriver检查单选按钮?

[英]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: 我的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>

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

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. 您可以通过id轻松找到该元素,然后只需调用isSelected方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查单选按钮是否在 Selenium WebDriver 中被选中? - How to check if the radio button is selected or not in Selenium WebDriver? 如何在Selenium Webdriver中选择单选按钮? 动态选择单选按钮 - How to select radio button in selenium webdriver ? dynamic select of radio button 如何使用Selenium WebDriver检查按钮是否可单击 - How to check if the button is clickable using selenium webdriver 如何使用 selenium webdriver 在 JQGrid 中选择单选按钮 - How to select Radio button in JQGrid using selenium webdriver 如何使用带有Java的Selenium WebDriver选择单选按钮? - How to select radio button using Selenium WebDriver with Java? 如何使用 Selenium WebDriver 读取 ::after 单选按钮元素的属性 - How to read ::after attributes of a radio button element using Selenium WebDriver 如何使用 java 在 selenium webdriver 中使用 select 单选按钮和复选框? - how to select radio button & checkbox in selenium webdriver with java? 如何通过不使用x路径来选择Selenium Webdriver中的单选按钮 - how to select radio button in selenium webdriver by not using x path 无法在Selenium Webdriver中单击单选按钮 - Unable to click on a radio button in Selenium Webdriver 使用Selenium Webdriver选择PrimeFaces单选按钮 - Select a PrimeFaces radio button with Selenium Webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM