Web表中有四列。“版本”,“下载”,“名称”,“上次访问”。
因此,如果我想在下载列中获取具有下载最大(值)的版本号。
如何使用硒实现这一目标。
答案 0 :(得分:0)
首先,您需要在List中存储元素:
List<WebElement> myList=driver.findElements(By.className("acc-toggle"));
//To store all web elements into list
List<String> all_elements_text=new ArrayList<>();
//If you want to get all elements text into array list
for(int i=0; i<myList.size(); i++){
all_elements_text.add(myList.get(i).getText());
//loading text of each element in to array all_elements_text
System.out.println(myList.get(i).getText());
}
然后您可以使用Collections API打印最大数量,如下所示:
Object obj = Collections.max(all_elements_text);
System.out.println(obj);
希望它会对你有所帮助。
答案 1 :(得分:0)
您可以尝试以下内容:
static int max=0;
static String version = null;
List<WebElement> storeRows = storesTable.findElements(By.cssSelctor("table tr"));
for(int i=0; i<storeRows.size();i++{
WebElement columnValue = storeRows.findElement(By.xpath("\td[3]"));
WebElement versionElem = storeRows.findElement(By.xpath("\td[1]"));
if(Integer.parseInt(columnValue.getText()) > max){
version = versionElem .getText();
max = Integer.parseInt(columnValue.getText());
}
}
System.out.println("For Vrsion " + version + " are the highest downloads = " + max);
答案 2 :(得分:0)
首先,您需要从Web表中读取版本和相应的下载次数。下面的代码就是这样:
// Below line will get column headers and rows of webtable
List<WebElement> cols = driver.findElements(By
.xpath(".//table/thead/tr/td/div"));
List<WebElement> rows = driver.findElements(By
.xpath(".//table/tbody/tr"));
Map<String, Integer> records = new HashMap();
// Add version column data into an array list
String version = "";
int downloadCount = 0;
for (int rowCount = 0; rowCount < rows.size(); rowCount++) {
List<WebElement> rowCells = rows.get(rowCount).findElements(
By.tagName("td"));
for (int count = 0; count < cols.size(); count++) {
// Get version into list
if (cols.get(count).getText().contains("Version")) {
version = rowCells.get(count)
.findElement(By.tagName("div")).getText();
}
// Get downloads into list
if (cols.get(count).getText().contains("Downloads")) {
downloadCount = Integer.parseInt(rowCells.get(count)
.findElement(By.tagName("div")).getText());
}
records.put(version, downloadCount);
}
}
接下来,您需要找到最大下载次数。使用流概念,你可以得到如下:
// This will get max download value
Optional<Entry<String, Integer>> str2 = versions3.entrySet().stream()
.max(new Comparator<Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1,
Entry<String, Integer> o2) {
if (o1.getValue() > o2.getValue()) {
return 0;
} else {
return -1;
}
}
});
// Max download value
System.out.println(str2.get().getValue());
// Corresponding versionid
System.out.println(str2.get().getKey());
假设有多个记录具有相同的下载次数(最大值),则下面的代码将获取所有版本。需要操纵字符串才能获得versionid。
//If there are more than one version with same downlaod value (Max) then below code will fetch all versions
List<Object> str4 = versions3.entrySet().stream()
.filter(entry -> entry.getValue() == str2.get().getValue())
.collect(Collectors.toList());
System.out.println(str4);
希望这有帮助。