我用ArrayList和HashSet编写了下面的代码。 但是哈希集没有按预期工作,这意味着它不会删除重复的项目。 但哈希集不允许重复。
**HTML** code
<HTML>
<BODY>
<select id= 'WesInn'>
<option value = 'idli'> IDLI</option>
<option value = 'vada'> VADA</option>
<option value = 'sambhar'> SAMBHAR</option>
<option value = 'Manchurian'> MACHURIAN</option>
<option value = 'idli'> IDLI</option>
<option value = 'sambhar'> SAMBHAR</option>
<option value = 'Tea'> TEA</option>
</select>
</body>
</html>
public class Assgn_DropDownAsc {
//static String text;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("C:\Users\admin\Desktop\New folder/Dropdown.html");
List list = new ArrayList ();
LinkedHashSet hs = new LinkedHashSet();
Adding ArrayList elements to the HashSet
//in order to remove the duplicate elements and
//to preserve the insertion order.
hs.addAll(list);
//System.out.println(hs);
//Removing ArrayList elements
hs.clear();
System.out.println(hs);
System.out.println(list);
//Adding LinkedHashSet elements to the ArrayList
hs.addAll(list);
//System.out.println(alloptions.size());
System.out.println(list);
}
}
输出:
[ 伊德利 瓦达 桑巴尔 马丘里安 伊德利 桑巴尔 茶]
如果您存储在输出中提到的字符串,您的程序运行良好,看起来您正在打印列表而不是哈希集 - 将System.out.println(list);
更改为System.out.println(hs);
只有在存储类的对象时才重要,在这种情况下,您应该正确覆盖 equals(( 和 hashCode(( 方法以使用 hashSet。
你可以试试这个:
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("C:\Users\admin\Desktop\New folder/Dropdown.html");
ArrayList<String> al= new ArrayList<String>();
al.add("IDLI");
al.add("VADA");
al.add("SAMBHAR");
al.add("MACHURIAN");
al.add("IDLI");
al.add("SAMBHAR");
al.add("TEA");
System.out.println("Array list : "+ al);
LinkedHashSet hs = new LinkedHashSet();
hs.addAll(al);
System.out.println("hash set :" +hs);
}
输出:
Array list : [IDLI, VADA, SAMBHAR, MACHURIAN, IDLI, SAMBHAR, TEA]
hash set :[IDLI, VADA, SAMBHAR, MACHURIAN, TEA]
以下是如何使用Selenium webdriver将 HashSet 和LinkedHashSet与List<WebElement>
一起使用的两个示例:
Java HashSet 类
Java HashSet 类用于创建使用哈希表进行存储的集合。它继承了 AbstractSet 类并实现了 Set 接口。
-
关于Java HashSet类的要点是:
哈希- 集使用称为哈希的机制存储元素。
- 哈希集仅包含唯一元素。 列表
- 和集合之间的区别:列表可以包含重复元素,而集合仅包含唯一元素。
-
哈希集类声明:
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable
-
示例(在谷歌搜索页中(:
List <WebElement> my_list = driver.findElements(By.xpath("//div[@id='rso']//div[@class='srg']/div[@class='g']//h3/a")); HashSet<WebElement> values = new HashSet<>(my_list); for(WebElement value:values) System.out.println(value.getAttribute("innerHTML"));
-
控制台输出:
Selenium (software) - Wikipedia Introduction — Selenium Documentation Selenium Tutorial Selenium Testing for Enterprise End-to-End Tests - Tricentis Selenium - Web Browser Automation Free Selenium Tutorials - Guru99 Downloads - Selenium Selenium IDE Selenium with Python — Selenium Python Bindings 2 documentation
Java LinkedHashSet 类
Java LinkedHashSet 类是 Set 接口的 Hash 表和链表实现。它继承了 HashSet 类并实现了 Set 接口。
-
关于Java HashSet类的要点是:
- 仅包含像 HashSet 这样的唯一元素。
- 提供所有可选的集合操作,并允许空元素。
- 保持广告顺序。
- LinkedHashSet 类的层次结构。
- LinkedHashSet 类扩展了实现 Set 接口的 HashSet 类。Set 接口按层次结构顺序继承集合和接口。
-
LinkedHashSet 类声明:
public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, Serializable
-
示例(在谷歌搜索页中(:
List <WebElement> my_list = driver.findElements(By.xpath("//div[@id='rso']//div[@class='srg']/div[@class='g']//h3/a")); LinkedHashSet<WebElement> values = new LinkedHashSet<>(my_list); for(WebElement value:values) System.out.println(value.getAttribute("innerHTML"));
-
控制台输出:
Selenium - Web Browser Automation Selenium IDE Downloads - Selenium Introduction — Selenium Documentation Free Selenium Tutorials - Guru99 Selenium (software) - Wikipedia Selenium with Python — Selenium Python Bindings 2 documentation Selenium Tutorial Selenium Testing for Enterprise End-to-End Tests - Tricentis
Here is the solution I got.
package qsp;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class DropdownAscen {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("C:\Users\admin\Desktop\New folder/Dropdown.html");
WebElement listbox = driver.findElement(By.id("WesInn"));
Select sel = new Select(listbox);
List<WebElement> alllist = sel.getOptions();
int count = alllist.size();
System.out.println(count);
/*for(int i=count-1;i>0;i--)
{
System.out.println(alllist.get(i).getText());
}*/
//Set hs = new HashSet<>(); /we will use this when we don't allow duplicate but not maintain insertion order
Set<String> hs = new TreeSet<>(); //we will use this to maintain duplication & sorting order
for(int i =0; i<count;i++)
{
WebElement option = alllist.get(i);
String txt = option.getText();
hs.add(txt);
//System.out.println(txt);
}
for(String sortedLst:hs)
{
System.out.println(sortedLst);
}
}}
输出
伊德利 马丘里安 桑巴尔 茶 瓦达