Friday, 14 February 2014

How to verify if the radio button is selected in selenium webdriver in Java?

Selenium web driver provides one method called - isSelected which can be used to check if the radio button is selected or not in Selenium Webdriver in Java.

boolean actualValue = e.isSelected();
If  radio button is selected , above code will return true Otherwise it will return false

Full example in Java with selenium webdriver

package temp;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class first {

public static void main(String[] args) {
              // TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver""C:\\Selenuim\\chromedriver2.3.exe");
WebDriver driver =  new ChromeDriver();

try{
driver.get("http://register.rediff.com/register/register.php");

Thread.sleep(2000);
WebElement e = driver.findElement(By.name("gender"));

boolean actualValue = e.isSelected();

if (actualValue)
       System.out.println("Radio Button is selected");
else
       System.out.println("Radio Button is not selected");
      
Thread.sleep(2000);

}

catch(Exception ex){
       System.out.println("Exception " + ex.getMessage());
              }
              finally{
                    
                     driver.close();
                     driver.quit();
              }
       }

}


Thus we can verify if the  radio button is selected or not using isSelected method in Java using selenium webdriver.

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books

Contributors