我想选择任何基本上放置在 div tag 中的随机菜单/选项。但是当我每次运行我的代码时,它只选择一个选项,即 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();
}
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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句