0

I'd like to use for loop for these to click them from 1st checkbox - 4th checkbox for my test automation.

webdriver.findElement(By.cssSelector("#mat-checkbox-1 .mat-checkbox-inner-container")).click();
webdriver.findElement(By.cssSelector("#mat-checkbox-2 .mat-checkbox-inner-container")).click();
webdriver.findElement(By.cssSelector("#mat-checkbox-3 .mat-checkbox-inner-container")).click();
webdriver.findElement(By.cssSelector("#mat-checkbox-4 .mat-checkbox-inner-container")).click();

I've tried the code below but it's still not working.

for(int i=1; i>=4; i++){
webdriver.findElement(By.cssSelector("#mat-checkbox-"+i+".mat-checkbox-inner-container")).click();
}
3
  • Is the test passing when you use the first quote and then test fails when you interchange the quote with the latter? Are you using the first quote in this exact manner or you have other logic between the four webdriver.findElement operations? Commented Jul 2, 2019 at 9:39
  • 1
    please do not edit your question with fixes, especially if it renders obsolete some of the given answers. Commented Jul 2, 2019 at 9:48
  • I have used the wrong sign, as it should be <= in the for loop. Thanks :) Commented Jul 2, 2019 at 10:51

1 Answer 1

4

First of all, your loop is not correct. The second condition is about when the loop should run, and since you have it as i>=4 and start at i=1 it would never run. Also, you should separate conditions with ;, so the correct for-loop in your case would be: for(int i = 1; i <= 4; i++){...}. And finally, you forgot the whitespace before .mat-checkbox-.. part. The correct code should look like this:

for(int i = 1; i <= 4; i++){
    webdriver.findElement(By.cssSelector("#mat-checkbox-"+i+" .mat-checkbox-inner-container")).click();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.