我正在尝试测试是否选择了 ROUND TRIP。我可以看到 ROUND TRIP 已被选中,但我仍然得到错误的输出,为什么?
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());
}
最佳答案
id 为round_trip_button1
的元素是一个a
元素,但您需要一个具有radio
类型的实际input
相反:
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');
关于java - isSelected() 方法在 selenium 代码中返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28805126/