如何从div标签中选择任何随机菜单/选项?

时间:2018-04-25 09:26:16

标签: java selenium-webdriver

我想选择任何基本上放在div标签中的随机菜单/选项。 但是当我每次运行我的代码时它只选择一个选项,即Goal5,而不是选择另一个选项! 我该怎么办?

这是我的HTML代码:

<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>

这是我的硒代码:

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();
}

3 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:0)

尝试使用以下方法。使用实用程序方法生成随机数。

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;
}

在代码中调用此实用程序方法。

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();
     }

答案 2 :(得分: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();