【问题标题】:isSelected() method return false in selenium codeisSelected() 方法在 selenium 代码中返回 false
【发布时间】:2019-01-18 00:16:54
【问题描述】:

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

    }

}

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

    id 为round_trip_button1 的元素是a 元素,但您需要一个实际的inputradio 类型:

    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');
    

    【讨论】:

      【解决方案2】:

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

      但在您的情况下,它是通过“a”(锚文本)实现的,因此默认 isSelected() 将不起作用。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-15
        • 2014-10-13
        • 1970-01-01
        • 2012-06-24
        • 1970-01-01
        • 2017-06-25
        • 2015-12-21
        • 1970-01-01
        相关资源
        最近更新 更多