import java.util.HashSet;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Demo27 {
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("file:///C:/Users/Administrator/Desktop/listDropdown.html");
WebElement listElement=driver.findElement(By.id("countrySelect"));
Select select=new Select(listElement);
List<WebElement> elements=select.getOptions();
int count=elements.size();
HashSet<String> set=new HashSet<String>();
for(int i=0;i<count;i++)
{
WebElement singleElement=elements.get(i);
String text=singleElement.getText();
if(!set.add(text))
{
System.out.println(text+"is duplicated");
}
}
driver.close();
}
}
答案 0 :(得分:0)
你可以用这种方式写一个逻辑。我创建了一个ArrayList
来捕获重复的值。如果在重复列表中找到传入的文本,我将提供它以供参考。如果它不存在,那么我将打印它。否则,我会忽略。这样就可以抑制要打印的第二个重复文本
boolean isSet = false;
List<String> duplicates = new ArrayList<String>();
if((isSet = set.add(text))){
//this will get printed for only unique values added
System.out.println(text+"is unique");
}
else{
if(!duplicates.contains(text)){
duplicates.add(text);
System.out.println(text+"is first duplicate");
}
}