How do I select a random checkbox on the web page?



  • I have 3 checkboxes on the webpage. Each time, I want to select checkboxes randomly.



  • You can use findElements to have a list of checkboxes then you simply have to pick a random element:

    List<WebElement> checkboxes = driver.findElements(By.cssSelector(".myCheckboxClass"));
    
    WebElement randomCheckbox = checkboxes.get(new Random().nextInt(list.size()));
    

    Note: It assumes some element will be found by the locator - if no element is found, an IndexOutOfBoundsException is raised.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2