[英]How to click on random item in the list Selenium Java
很抱歉這個問題,我明白,對於某些人來說這很容易,但我需要幫助
例如我有:
@FindBy(xpath="example")
private List<WebElement> exampleList;
我需要點擊列表中的隨機項目:
public void clickOnRandomItemInList() {
exampleList.get(i).click; //how I can randomize "I"
}
我試過這個:
Random randomize = new Random()
public void clickOnRandomItemInList() {
exampleList.get(randomize.nextInt(exampleList.size)).click; //but this way doesn't work
}
你可以這樣做:
根據List大小獲取一個隨機索引,相應地從List中獲取元素並點擊它。
public void clickOnRandomItemInList(){
Random rnd = new Random();
int i = rnd.nextInt(exampleList.size());
exampleList.get(i).click();
}
我們需要確定lower limit
和upper limit
,然后在兩者之間生成一個數字。
我們將下限設置為 1,因為我們至少要處理 1 個 web 元素。
上限我們將使用列表大小, int high = exampleList.size();
現在使用下面的代碼
Random r = new Random();
int low = 1;
int high = exampleList.size();
int result = r.nextInt(high-low) + low;
現在調用這個方法
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低是(inclusive)
和高是(exclusive)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.