isSelected()方法在selenium代码中返回false

时间:2015-03-02 08:09:40

标签: java selenium selenium-webdriver

我正在尝试测试是否选择了ROUND TRIP。我可以看到ROUND TRIP被选中,但我仍然得到输出为false为什么?

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class IsSelected {
public static void main(String[] args) {
    WebDriver f= new FirefoxDriver();
    f.get("http://www.makemytrip.com/");
    WebElement e = f.findElement(By.id("round_trip_button1"));
    System.out.println(e.isSelected());
    System.out.println(e.getText());
    System.out.println(e.isDisplayed());
    System.out.println(e.isEnabled());

    }

}

2 个答案:

答案 0 :(得分:3)

标识为round_trip_button1的元素是a元素,但您需要实际input radio类型:

f.findElement(By.cssSelector("a#round_trip_button1 input[type=radio]"));

UPD:要关注@aberry回答 - 您需要检查a标记以获得active课程。定义function that would check for an element to have a class

public static bool hasClass(WebElement el, string className) {
    return el.getAttribute("class").split(' ').contains(className);
}

并使用它:

hasClass(f.findElement(By.id("round_trip_button1")), 'active');

答案 1 :(得分:1)

isSelected()适用于输入 复选框等元素,选择单选按钮中的选项。

但在你的情况下,它是通过' a'来实现的。 (锚文本)所以默认的isSelected()将不起作用。

对于你的情况,我查了一下'属性,您可以通过检查类值' active'轻松自定义实现isSelected()方法。 当选择round_trip_button1 id时,其类包含字符串' active'和其他情况“活跃的'不见了。