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
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) {
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.