简体   繁体   中英

isSelected() method return false in selenium code

I am trying to test whether ROUND TRIP is selected or not. I can see ROUND TRIP is selected but still i am getting output as false why?

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

    }

}

The element with round_trip_button1 id is an a element, but you need an actual input with radio type instead:

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

UPD: to follow @aberry answer - you need to check an a tag to have an active class. Define a 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);
}

And use it:

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

isSelected() applies to input elements such as checkboxes , options in a select and radio buttons.

But in your case, it is implemented through 'a' (anchor text) so default isSelected() will not work.

For your case I checked 'a' properties, you can easily custom implement isSelected() method by checking class value 'active'. When round_trip_button1 id is selected, its class contains string 'active' and other case 'active' is missing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM