라디오 버튼을 선택하고 체크하세요Box in Selenium

라디오 버튼 있음 Selenium

라디오 버튼 역시 click() 메서드를 사용하여 켤 수 있습니다.

사용 https://demo.guru99.com/test/radio.html 연습을 위해 radio1.click()이 "Option1" 라디오 버튼을 켜고, radio2.click()이 "Option2" 라디오 버튼을 켜고 "Option1"은 선택되지 않은 상태로 둡니다.

라디오 버튼 입력 Selenium

체크박스를 선택하는 방법 Selenium

Toggl체크박스를 켜거나 끄는 것도 다음을 사용하여 수행됩니다. 딸깍 하는 소리() 방법.

아래 코드는 Facebook의 "로그인 상태 유지" 체크 박스를 두 번 클릭한 후, 체크 박스가 켜져 있으면 결과를 TRUE로 출력하고, 꺼져 있으면 결과를 FALSE로 출력합니다.

체크박스 선택 Selenium

체크박스 선택 Selenium

isSelected() 메서드는 체크박스가 켜져 있는지 꺼져 있는지 아는 데 사용됩니다.

다음은 또 다른 예입니다. https://demo.guru99.com/test/radio.html

체크박스 선택 Selenium

완전한 코드

전체 작업 코드는 다음과 같습니다.

import org.openqa.selenium.By;		
import org.openqa.selenium.WebDriver;		
import org.openqa.selenium.chrome.ChromeDriver;		
import org.openqa.selenium.*;		

public class Form {				
    public static void main(String[] args) {									
    		
    	// declaration and instantiation of objects/variables		
        System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");					
        WebDriver driver = new ChromeDriver();					

        driver.get("https://demo.guru99.com/test/radio.html");					
        WebElement radio1 = driver.findElement(By.id("vfb-7-1"));							
        WebElement radio2 = driver.findElement(By.id("vfb-7-2"));							
        		
        //Radio Button1 is selected		
        radio1.click();			
        System.out.println("Radio Button Option 1 Selected");					
        		
        //Radio Button1 is de-selected and Radio Button2 is selected		
        radio2.click();			
        System.out.println("Radio Button Option 2 Selected");					
        		
        // Selecting CheckBox		
        WebElement option1 = driver.findElement(By.id("vfb-6-0"));							

        // This will Toggle the Check box 		
        option1.click();			

        // Check whether the Check box is toggled on 		
        if (option1.isSelected()) {					
            System.out.println("Checkbox is Toggled On");					

        } else {			
            System.out.println("Checkbox is Toggled Off");					
        }		
         
        		
        		
        //Selecting Checkbox and using isSelected Method		
        driver.get("https://demo.guru99.com/test/facebook.html");					
        WebElement chkFBPersist = driver.findElement(By.id("persist_box"));							
        for (int i=0; i<2; i++) {											
            chkFBPersist.click (); 			
            System.out.println("Facebook Persists Checkbox Status is -  "+chkFBPersist.isSelected());							
        }		
		//driver.close();		
        		
    }		
}

문제해결

요소를 찾는 동안 NoSuchElementException()이 발생하면 웹 드라이버가 페이지에 액세스한 시점에 해당 요소가 페이지에서 발견되지 않았음을 의미합니다.

  1. Chrome에서 Firepath 또는 Inspect Element를 사용하여 로케이터를 다시 확인하세요.
  2. 코드에 사용한 값이 현재 Firepath의 요소 ​​값과 다른지 확인하세요.
  3. 일부 속성은 몇몇 요소에 대해 동적입니다. 값이 다르고 동적으로 변경되는 경우, 더 안정적이지만 복잡한 방법인 By.xpath() 또는 By.cssSelector()를 사용하는 것을 고려하세요.
  4. 때로는 대기 문제일 수도 있습니다. 즉, 페이지가 완전히 로드되기 전에 웹 드라이버가 코드를 실행한 경우 등이 있습니다.
  5. 암시적 또는 명시적 대기를 사용하여 findElement() 앞에 대기를 추가합니다.

제품 개요

  • 아래 표는 위에서 논의된 각 유형의 요소에 액세스하기 위한 명령을 요약한 것입니다.
요소 Command 기술설명
체크 Box, 라디오 버튼 딸깍 하는 소리() 요소를 켜거나 끄는 데 사용됩니다.