簡體   English   中英

硒:換算表 <WebElement> 設置 <String>

[英]Selenium : Converting List<WebElement> to Set<String>

我想從頁面中獲得獨特的元素。 我正在計算記錄總數。 每個記錄都屬於一個用戶,因此有多個記錄屬於同一用戶。 我想獲得具有唯一用戶數的總數。

List<WebElement> efirstpagecount = driver.findElements(By.xpath("//*[@id='usersList']/tbody/tr/td[3]"));

Set<WebElement> uniquecount = new HashSet<WebElement>(efirstpagecount);
System.out.println("Unique count: " + uniquecount.size());

for (WebElement u : uniquecount ) {
  System.out.println(u.getText());
  }

輸出:

Unique count: 20
robin
Rocky
prom
jack
stone
Veronica
Veronica
Shawn
Rocky
carl
Rocky
James
Rocky
sam
bon
sam
bone
don
Shawn
don

上面的代碼為我提供了包括重復值在內的計數。 請告知如何獲取唯一值。 提前致謝!

假設td僅具有用戶名,則可以在Java 8中嘗試類似的方法。

    Set<String> uniqueUsers = efirstpagecount.stream()
            .map(WebElement::getText).map(String::trim)
            .distinct().collect(Collectors.toSet());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM