Mark H
Mark H

Reputation: 91

Test if button selected based

I need to locate a button , which may or may not be selected.

I first find the xpath to the button, check is its selected, it is failing in this check "Unable to locate element: {"method":"tag name","selector":"input"}"

What should my boolean line check be ?

  WebElement element =  driver.findElement(By.xpath("//i[contains(@class,'i-icon--plus-18-movie')]"));

    boolean isChecked = element.findElement(By.tagName("input")).isSelected();
        if(isChecked){
            System.out.println("is checked");
                 element.click();
        }  


<age-question-button class="ng-scope ng-isolate-scope" state="qc.answer[question.name]" icon="plus-18-movie" active-text="The Customer is over 18" inactive-text="The Customer is NOT over 18" ng-repeat="question in qc.questionsList track by question.name" audit="cc.utils.audit(qc.answer[question.name] ? question.auditInactive : question.auditActive)">
<label class="c-option u-p-0 u-ml-md u-pull-left" tooltip-append-to-body="true" uib-tooltip="The Customer is NOT over 18">
<input class="ng-untouched ng-valid ng-dirty ng-valid-parse" ng-model="state" ng-change="audit()" style="" type="checkbox">
<i class="c-option__button i-icon i-icon--plus-18-movie"></i>
</label>
</age-question-button>

Upvotes: 1

Views: 119

Answers (2)

Mahmud Riad
Mahmud Riad

Reputation: 1165

Please try to avoid multiple class in same path. You can use like below

WebElement elem = driver.findElement(By.className("ng-isolate-scope"));
WebElement element =  elem.findElement(By.className("ng-valid-parse"));

boolean isChecked = element.findElement(By.tagName("input")).isSelected();
if(isChecked){
    System.out.println("is checked");
    element.click();
}        

IF you have multiple classes with same name you can use list and desired Index of that element.

WebElement elem = driver.findElement(By.className("ng-isolate-scope"));
List<WebElement> element =  elem.findElements(By.className("ng-valid-parse"));

boolean isChecked = element.get(indexOfyourdesiredELement).findElement(By.tagName("input")).isSelected();
if(isChecked){
       System.out.println("is checked");
       element.get(indexOfyourdesiredELement).click();
}  

You can also write like

boolean isChecked =  element.findElement(By.cssSelector("input[type='checkbox']")).isSelected();

EDIT: Here is the updated solution . It will click on the checkbox if it is checked. You can reverse the logic by yourself.

WebElement element = driver.findElement(By.tagName("age-question-button"));
boolean isChecked =  element.findElement(By.cssSelector("input[type='checkbox']")).isSelected();
if(isChecked){
    System.out.println("is checked");
    element.click();
}  

If multiple age-question-button question are there follow this.

List<WebElement> element = driver.findElements(By.tagName("age-question-button"));

    for(int i = 0 ; i< element.size() ; i++){
        boolean isChecked =  element.get(i).findElement(By.cssSelector("input[type='checkbox']")).isSelected();
        if(isChecked){
              System.out.println("is checked and it will click on checkbox if it is checked");

               element.get(i).click();
        }  
    }

Upvotes: 2

Ali Azam
Ali Azam

Reputation: 2115

Try using cssSelector input[type='checkbox']

WebElement element =  driver.findElement(By.cssSelector("input[type='checkbox']"));
boolean isChecked = element.isSelected();
    if(isChecked){
        System.out.println("is checked");
             element.click();
    } 

Upvotes: 0

Related Questions