0

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.
http://prntscr.com/106nuzd

2 Answers 2

1
    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

0

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) {
    a.get(i).click();
}

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.

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

Your Answer

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

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