2014-11-10 3 views

답변

0

다음 코드를 사용하십시오. 체크 박스가 체크되어 있는지 여부에 따라 부울 값을 반환합니다. 그것을 바탕으로 체크 박스를 클릭 할 수 있습니다. 행복한 코딩.

boolean selected = element.isSelected(); 
     if(!selected) 
     { 
      element.click(); 
     } 
0

사용이 만약 당신이 :

bool flag = false; 
     if (checkBox1.Checked == true) 
      flag = true; 
0
//Checking 
public void CheckingChkbox(WebElement chkbx1){ 
boolean checkstatus; 
checkstatus=chkbx1.isSelected(); 
if (checkstatus==true){ 
    System.out.println("Checkbox is already checked"); 
} 
else 
{ 
    chkbx1.click(); 
    System.out.println("Checked the checkbox"); 
} 
} 

//Unchecking 
public void UnCheckingChkbox(WebElement chkbx1){ 
boolean checkstatus; 
checkstatus=chkbx1.isSelected(); 
if (checkstatus==true) { 
chkbx1.click(); 
    System.out.println("Checkbox is unchecked"); 
} 
else 
{ 
    System.out.println("Checkbox is already unchecked"); 
} 
} 

http://seleniumcodes.blogspot.in/2011/10/checking-and-unchecking-checkbox.html

+0

감사합니다. <입력 ID = "role_checkbox_1"클래스 = "checkboxtreeHTMLCheckBox"유형 = "체크 박스"값 = "선택 해제"/>.이 html 코드를 사용하여, 사용하는 방법 말해. – Vids

4

하는 방법이있다 "의 IsEnabled()"는 WebElement가를 사용 여부를 확인하는. 아래 코드를 사용하여이를 확인할 수 있습니다.

boolean enabled = driver.findElement(By.xpath("//xpath of the checkbox")).isEnabled(); 

( 나는 위의 XPath를 사용하고 있지만, 너무 요소를 찾기위한 ID 또는 cssselector를 사용할 수 있습니다. 를)

위의 코드는 '사실'반환합니다 당해 경우 WebElement, 즉 확인란의 경우 활성화되어 있습니다. 그렇지 않으면 'false'가 반환됩니다.

에 확인란이 선택되어 있는지 여부를 확인하려면 "isSelected()"메서드을 사용할 수 있습니다.

boolean checked = driver.findElement(By.xpath("//xpath of the checkbox")).isSelected(); 

위의 코드는 우려 WebElement가, 즉, 체크 박스 귀하의 경우, 선택된 경우, 다른 사람이 'false'로 돌아갑니다 '사실'반환합니다. 이 체크 박스를 선택하면 문자열 "패스"를 반환하고,이 외설이 "실패"할

- : 주석에 코드를 촬영


, 나는 아래의 방법을 연상했다 이 코드를 실행하는 동안 오류가 발생했는지 확인했습니다. 코더 호크 @

public static String isCheckBoxChecked(String objlocator, String elemName) { 

     APP_LOGS.debug("Checking if the checkbox related to '"+elemName+"' is checked or not."); 
     System.out.println("Checking if the checkbox related to '"+elemName+"' is checked or not."); 

     try { 

      findWebElement(objlocator); 

      //Assuming the objLocator contains xpath 
      if (driver.findElement(By.xpath(objlocator)).isSelected()) { 
       System.out.println("Checkbox related to: '"+elemName+"' is checked."); 
       APP_LOGS.debug("Checkbox related to: '"+elemName+"' is checked."); 
      }else{ 
       System.out.println("Checkbox related to: '"+elemName+"' is not checked!!"); 
       APP_LOGS.debug("Checkbox related to: '"+elemName+"' is not checked!!"); 
       return "Fail" + ": Checkbox related to: '"+elemName+"' is not checked!!"; 
      } 
     } 
     catch (Throwable t) { 
      System.out.println("Error while Checking if the checkbox related to '"+elemName+"' is checked or not. -" + t.getMessage()); 
      APP_LOGS.error("Error while Checking if the checkbox related to '"+elemName+"' is checked or not. -" + t.getMessage()); 
      return "Fail"+": Error while Checking if the checkbox related to '"+elemName+"' is checked or not. -" + t.getMessage(); 
     } 

     return "Pass"+": Checkbox related to: '"+elemName+"' is checked."; 
    } 
관련 문제