-1

I want to select any random menu/option which is basically placed in div tag . but when i run my code every time it only select one option which is Goal5 instead of selecting another option !! What should i do ??

Here's my HTML code:

<div id="AddNewGoalContainer" class="clearfix row goal-boxes-container">
<div id="goal-GOAL001" class="col-xs-6 goal-box-holder new-goal-buttons">Goal1</div>
<div id="goal-GOAL002" class="col-xs-6 goal-box-holder new-goal-buttons">Goal2</div>
<div id="goal-GOAL003" class="col-xs-6 goal-box-holder new-goal-buttons">Goal3</div>
<div id="goal-GOAL004" class="col-xs-6 goal-box-holder new-goal-buttons">Goal4</div>
<div id="goal-GOAL005" class="col-xs-6 goal-box-holder new-goal-buttons">Goal5</div>
<div id="goal-GOAL006" class="col-xs-6 goal-box-holder new-goal-buttons">Goal6</div>
<div id="goal-GOAL007" class="col-xs-6 goal-box-holder new-goal-buttons">Goal7</div>
<div id="goal-GOAL008" class="col-xs-6 goal-box-holder new-goal-buttons">Goal8</div>
<div id="goal-GOAL009" class="col-xs-6 goal-box-holder new-goal-buttons">Goal9</div>
<div id="goal-GOAL010" class="col-xs-6 goal-box-holder new-goal-buttons">Goal10</div>
</div>

Here's my selenium code:

List<WebElement> myGoal = driver.findElements(By.id("AddNewGoalContainer"));

     List<String> all_elements_text=new ArrayList<>();

        for (int i = 0; i < myGoal.size(); i++) 
        {
            all_elements_text.add(myGoal.get(i).getText());
            System.out.println(myGoal.get(i).getText());
            int size= myGoal.size();
            //System.out.println(size);
            int randoumNumber = ThreadLocalRandom.current().nextInt(0, size);
            myGoal.get(randoumNumber).click();
}
JeffC
  • 22,180
  • 5
  • 32
  • 55
Prachi Dalvi
  • 1
  • 1
  • 4
  • Possible duplicate of [How do I generate random integers within a specific range in Java?](https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java) – JeffC Apr 25 '18 at 13:04

3 Answers3

0
  List myGoal = $('.new-goal-buttons').length;

  List<String> all_elements_text=new ArrayList<>();

for (int i = 1; i < myGoal; i++) 
{
    all_elements_text.add(myGoal.get(i).getText());
    System.out.println(myGoal.get(i).getText());
    int size= myGoal.size();
    //System.out.println(size);
    int randoumNumber = ThreadLocalRandom.current().nextInt(0, size);
    myGoal.get(randoumNumber).click();
}
0

Try out with the below approach. Use a utility method to generate a random number.

public static int getRandomNumber(int initial, int limit) {
    if (initial >= limit) {
        throw new IllegalArgumentException("limit should be greater than initial");
    }
    Random r = new Random();
    return r.nextInt((limit - initial) + 1) + initial;
}

Call this utility method inside your code.

List<WebElement> myGoal = driver.findElements(By.xpath("//div[starts-with[@id,'GOAL']));

 List<String> all_elements_text=new ArrayList<>();

    for (int i = 0; i < myGoal.size(); i++) 
    {
        all_elements_text.add(myGoal.get(i).getText());
        System.out.println(myGoal.get(i).getText());
        int size= myGoal.size();
        //System.out.println(size);
        int randoumNumber = UtilityClass.getRandomNumber(1,size);
        myGoal.get(randoumNumber).click();
     }
0
List<WebElement> myGoal = driver.findElements(By.xpath("//div[starts-with(@id,'goal-GOAL0')]"));// xpath 
    Random rr=new Random();
    System.out.println("Random number is "+rr);
    int randomGoal = rr.nextInt(myGoal.size());
    System.out.println("Goal :"+myGoal.get(randomGoal).getText());
    myGoal.get(randomGoal).click();
Prachi Dalvi
  • 1
  • 1
  • 4