//retrieving a set of WebElement
List <WebElement> discount =driver.findElements(By.className("saleD"));
for(int i=0;i<discount.size();i++)
{
//getting text of webelements
String disc_per= discount.get(i).getText();
// Now I want that only unique texts to be stored. What can I do to get this.
}
System.out.println(count);
答案 0 :(得分:0)
将List转换为Set,它将仅保留唯一值。
List<String> webElementText= Store all (webElement.getText)
Set<String> uniquewebElementsText = new HashSet<String>(webElementText);
现在uniquewebElementsText
包含所有唯一条目。
答案 1 :(得分:-1)
List#contains()
方法会为您提供。
ArrayList list = new ArrayList();
for(int i = 0; i < discount.size(); ++i)
{
if(!list.contains(discount.get(i))
list.add(discount.get(i));
}
System.out.println("No of unique elements are : " + list.size());
System.out.println(list);
或者您可以使用set
。
Set set = new Set();
for(int i = 0; i < discount.size(); ++i)
{
set.add(discount.get(i));
}
或1个班轮。
Set<String> s = new LinkedHashSet<String>(list);