Java 如何使用 Selenium WebDriver 检查单选按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25997365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:39:25 来源:igfitidea点击:
How to check a radio button with Selenium WebDriver?
提问by Sandy Maciel
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>
采纳答案by DSL
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");
}
}
}
回答by Javier92
This questionshould definitely help you.
这个问题肯定对你有帮助。
You can find the element easily by id, then you just have to call the isSelected
method.
您可以通过 id 轻松找到元素,然后您只需要调用该isSelected
方法即可。
回答by user3657330
Please try this code to select the radiobutton-
请尝试使用此代码选择单选按钮-
driver.findElement(By.cssSelector("input[id='optionStopGrid']")).click();