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이 켜졌다 꺼지며 콘솔창에는

위와 같은 결과가 나오는 것을 확인할 수 있다
'테스트툴 > Selenium' 카테고리의 다른 글
| Selenium Webdriver 링크 텍스트 요소 찾기 (0) | 2021.03.16 | 
|---|---|
| Selenium Webdriver 드롭다운에서 값 선택 (0) | 2021.03.16 | 
| Selenium Webdriver에서 이미지 클릭 (0) | 2021.03.16 | 
| Selenium Form WebElement (0) | 2021.03.14 | 
| Selenium WebDriver 설치 (0) | 2021.03.14 |