![](/img/trans.png)
[英]How to check if the radio button is selected or not in Selenium WebDriver?
[英]How to check a radio button with Selenium WebDriver?
我想檢查此單選按鈕,但我不知道如何。 我的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");
}
}
}
這個問題一定會對您有所幫助。
您可以通過id輕松找到該元素,然后只需調用isSelected
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.