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

标签 java selenium selenium-webdriver

我正在尝试测试是否选择了 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/

相关文章:

java - 如何从 List<Object> 中检索每个值

java - dotcms - 将看不到新的数据库配置

python - 无法获取所有子项(动态加载)selenium python

jenkins - 在 Jenkins 中为所有构建的单个测试用例执行时间绘制图形

python - 如何让 WebDriverWait 查找特定号码

java - "AS"相当于 sqlite

java - 为什么 0.5==0.5f 为真而 0.1==0.1f 为假?

c# - Selenium:实例化时为 "DevTools Request: 127.0.0.1:12583/json/version failed"

selenium - 如何在 Selenium 中按 shift + ctrl + s

testing - 如何使用 Selenium Webdriver 双击一个元素