Radio button checkbox is checked with selenium webdriver

selenium

To verify that radio button or the checkbox is checked or not we can use the following code.

package com.webdriver_13;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MyVerification_01 {

	public WebDriver driver;
	private String baseUrl;
	public static void main(String[] args) {
		MyVerification_01 mv = new MyVerification_01();
		mv.launch_test();
	}
	public void launch_test(){
		try{
			 driver = new FirefoxDriver();
			 baseUrl = "http://demo.tanmaysarkar.com/sample_03.html";
			 driver.get(baseUrl);
			 driver.manage().window().maximize();
			 System.out.println("Opening " + baseUrl);
			 
			 // Is radio button select ?
			 if(driver.findElement(By.xpath("html/body/form/table/tbody/tr[6]/td[2]/p/label[1]/input")).isSelected())
			 {
				 System.out.println("Gender is selected as Male");
			 }
			 else
			 {
				 System.out.println("Gender is NOT selected as Male");
			 }
			 
			 // Is check box checked ?
			 if(driver.findElement(By.xpath("//input[contains(@name,'ts_checkbox2')]")).isSelected())
			 {
				 System.out.println("Football is checked");
			 }
			 else
			 {
				 System.out.println("Football is NOT checked");
			 }
			 driver.quit();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}

and after successfully running the code you can get the following output –

Opening http://demo.tanmaysarkar.com/sample_03.html
Gender is selected as Male
Football is checked

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *