How to select random checkbox in a table because checkbox id, type , name are same then how to get random checkbox select and value is different.

2 Answers 2

    List<WebElement> a = driver.findElements(By.id("#checkbox"));
    a.get(new Random().nextInt(a.size())).click();

use findElements to get all checkbox and then use Random to generate a random index and click on that webelement in the list


You can use this code to select random number of checkboxes,

List<WebElement> a = driver.findElements(By.id("#checkbox"));
Random rand = new Random(); //instance of random class
int int_random = rand.nextInt(a.size()); 
for(int i = 0; i < a.size(); i += int_random) {

This will iterate through the list of all checkboxes with ID="checkbox" and then randomly increment the counter so that the loop doesn't go sequentially selecting all the checkboxes.

  • Ignore the previous comment ,
    – PDHide
    Feb 26, 2021 at 5:43
  • Thanks for the help
    – MS Demo
    Mar 19, 2021 at 7:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.