簡體   English   中英

嘗試檢查單選按鈕是否被選中

[英]Trying to check whether a radio button is checked or not

我正在嘗試檢查網站http://www.makemytrip.com/上是否選中了單選按鈕,但它始終顯示為 false。

public static void cBoxRbtnDd () throws Exception{
    driverGlobal.get("http://www.makemytrip.com/");
    driverGlobal.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    String rdBtn = ".//*[@id='one_way_button1']/span";
    boolean att = driverGlobal.findElement(By.xpath(rdBtn)).isSelected();
    System.out.println(att);

    driverGlobal.findElement(By.xpath(rdBtn)).click();

    WebElement radioBtn = driverGlobal.findElement(By.xpath(rdBtn));
    new WebDriverWait(driverGlobal,10).until(ExpectedConditions.visibilityOf(radioBtn));

    boolean att1 = driverGlobal.findElement(By.xpath(rdBtn)).isSelected();
    System.out.println(att1);   
}

單選按鈕通常使用帶有屬性type="radio"<input>創建。 如果您注意到站點上的單選按鈕,他們實際上是在使用<a>標簽和 DOM 操作來創建單選按鈕。

單選按鈕被選中的效果是由 CSS 完成的。 請注意,選定的單選按鈕有一個類active 因此,您需要檢查該類是否包含active值以了解它是否已被選中。

您可以使用下面給出的代碼片段:

public boolean ifActive(WebElement element) {
    String classes = element.getAttribute("class");
    return classes.contains("active");
}
ifActive(driverGlobal.findElement(By.id("one_way_button1")));    //false
ifActive(driverGlobal.findElement(By.id("round_trip_button1"))); //true
ifActive(driverGlobal.findElement(By.id("multi_city_button")));  //false

注釋中的值是函數將返回的值。 請注意,makemyrtip.com 最初默認選擇往返單選按鈕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM