[英]How to click on random item in the list Selenium Java
Sorry for this question, I understand, for somebody this is easy, but I need help很抱歉这个问题,我明白,对于某些人来说这很容易,但我需要帮助
For example I have:例如我有:
@FindBy(xpath="example")
private List<WebElement> exampleList;
And I need to click on the random item in the list:我需要点击列表中的随机项目:
public void clickOnRandomItemInList() {
exampleList.get(i).click; //how I can randomize "I"
}
I tried this one:我试过这个:
Random randomize = new Random()
public void clickOnRandomItemInList() {
exampleList.get(randomize.nextInt(exampleList.size)).click; //but this way doesn't work
}
You can do it as following:你可以这样做:
Get a random index according to the List size, get element from the List accordingly and click it.根据List大小获取一个随机索引,相应地从List中获取元素并点击它。
public void clickOnRandomItemInList(){
Random rnd = new Random();
int i = rnd.nextInt(exampleList.size());
exampleList.get(i).click();
}
We need to determine lower limit
and upper limit
and then generate a number in between.我们需要确定lower limit
和upper limit
,然后在两者之间生成一个数字。
lower limit we will set to 1, cause we at least wanna deal with 1 web element.我们将下限设置为 1,因为我们至少要处理 1 个 web 元素。
upper limit we will use list size, int high = exampleList.size();
上限我们将使用列表大小, int high = exampleList.size();
Now using the below code现在使用下面的代码
Random r = new Random();
int low = 1;
int high = exampleList.size();
int result = r.nextInt(high-low) + low;
and now call this method现在调用这个方法
public void clickOnRandomItemInList() {
Random r = new Random();
int low = 1;
int high = exampleList.size();
int result = r.nextInt(high-low) + low;
exampleList.get(result).click;
}
PS low is (inclusive)
and high is (exclusive)
PS低是(inclusive)
和高是(exclusive)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.