[英]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.