2015-11-06 107 views
0

我有ArrayListWebElement,我想從中刪除重複的字符串。我怎樣才能做到這一點?好心提醒。謝謝。如何從ArrayList Selenium WebDriver JAVA中刪除重複的WebElement?

HTML:

<li data-id="20151105031XJ500wh0001" data-istrace="0" class="evenTr first"> 
    <span class="wd1">2015-11-05 16:10:58</span> 
    <span class="wd5">3D福彩</span> 
    <span title="一碼不定膽">一碼不定膽</span> 
    <span class="wd2">2015302</span> 
    <span class="wd3">元</span> 
    <span class="grid-toggle" alt="號碼詳情:"> 
    <div class="wrapbox"> 
     <em alt="2">2</em> 
     </div> 
     </span> 
    <span class="wd4">1</span> 
    <span>¥2</span> 
    <span>¥0</span> 
    <span class="wd4">未開獎</span> 
    <span alt="開獎號碼:"></span> 
    <span class="wd3">否</span> 
</li> 

這裏是我的代碼:

List<String> spanText = new ArrayList<String>(); 
List<WebElement> we = driver.findElements(By.xpath("//li/span[@alt='號碼詳情:']")); 
Set<WebElement> hs = new LinkedHashSet<WebElement>(); 

hs.addAll(we); 
System.out.print("Value of addall" + hs + "----"); 
we.clear(); 
we.addAll(hs); 
System.out.print("Value of we" + we + "----"); 

for (WebElement w : we) 
{ 
    spanText.add(w.getText()); 
    System.out.print("Value of" + spanText + "done - "); 
} 

我得到了重複的結果,我只需要值:對話框元素的

Value of[2, 047, 查看全部, 28, 18, 18, 18, 28, 28, 28] 

圖片:

Image WebElement

回答

0

基本上要通過WebElementList在S環,看看.getText()已經包含在你的Set。如果不是,則添加它。

List<WebElement> elements = driver.findElements(By.xpath("//li/span[@alt='號碼詳情:']")); 
Set<String> hs = new LinkedHashSet<>(); 
for (WebElement element : elements) 
{ 
    if (!hs.contains(element.getText())) 
    { 
     hs.add(element.getText()); 
    } 
} 
相關問題