본문 바로가기

테스트툴/Selenium

Selenium WebDriver CheckBox, 라디오 버튼

http://demo.guru99.com/test/radio.html 로 실습

 

라디오 버튼은 click () 메서드를 사용하여 토글 할 수 있다

package newpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;//webElement 사용

public class MyClass {
	public static void main(String[] args){
		System.setProperty("webdriver.chrome.driver", "c:/selenium/chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		
		String getURL = "http://demo.guru99.com/test/radio.html";
		driver.get(getURL);
		
		WebElement radio1 = driver.findElement(By.id("vfb-7-1"));
		WebElement radio2 = driver.findElement(By.id("vfb-7-2"));
		
		radio1.click();
		radio2.click();
	}
}

radio1.click()로 option1이 선택되었다가

radio2.click()로 option1이 선택 해제되고, option2가 선택

 

 

체크박스 또한 click() 메서드를 사용하여 수행된다

package newpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;//webElement 사용

public class MyClass {
	public static void main(String[] args){
		System.setProperty("webdriver.chrome.driver", "c:/selenium/chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		
		String getURL = "http://demo.guru99.com/test/radio.html";
		driver.get(getURL);
		
		WebElement option1= driver.findElement(By.id("vfb-6-0"));
		option1.click();
		
		if(option1.isSelected()){
			System.out.println("chkBox Toggle On");
		}
		else{
			System.out.println("chkBox Toggle Off");
		}
		
		option1.click();
		if(!option1.isSelected()){
			System.out.println("chkBox now Toggle Off");
		}
	}
}

isSelected() 메서드로 CheckBox가 선택 되었는지 여부를 확인한다

위 코드로 option1이 켜졌다 꺼지며 콘솔창에는

콘솔

위와 같은 결과가 나오는 것을 확인할 수 있다