2016-05-13 72 views
0
String name = ""; 
    String width = ""; 
    String height = ""; 



    List<WebElement> imageName = driver.findElements(By.cssSelector("div.card-arago div.hover-info div.name")); 
    List<HashMap> imageInfo = new ArrayList<HashMap>(); 
    HashMap<String, String> attributes = new HashMap<String, String>(); 
    imageInfo.add(attributes); 
    for (WebElement image : imageName) { 
     attributes.put("name",image.getText()); 
    } 
    List<WebElement> images = driver.findElements(By.cssSelector("div.card-arago a img")); 
    for (WebElement image : images) { 
     attributes.put("width", image.getAttribute("width")); 
     attributes.put("height", image.getAttribute("height")); 

    } 

我试图从页面返回所有图像,但它只返回页面上的最后一张图像卡?Java Webdriver HashMap元素数组

回答

1

HashMap只能用每个键存储一个值。如果您多次拨打同一个密钥,请拨打put,每次通话都会覆盖前一个。您在一个循环中多次呼叫attributes.put("name", ...),因此附加到键“name”的值会一遍又一遍地被替换,并且在循环的最后只剩下最后一张图像。如果你真的想要返回所有的图像,你可能需要每个图像的唯一键,或者每个图像需要一个完全独立的HashMap,具体取决于你想要如何构造它。

编辑︰看你的代码多一点后,它看起来像你想要一个HashMap的列表。但是你只添加一个单一的HashMap到那个List。相反,您可以更改第一个循环以为每个图像添加一个新的HashMap。

+0

听起来像这将是最简单的方法。它将如何为每个图像添加一个新的HashMap? –

0

当调用put(k,v)时,密钥被存储并且值是额外的元数据。只是一个关键和价值。 如果我打电话(1,2);然后叫put(1,3); get(1)返回的值;将是3. put(k,v);函数只在HashMap中存储不同的对象。在同一个HashMap中你不能有相同的值,但是,你可以在同一个HashMap中拥有相同的值。

为了解决你的问题,我建议你使用这样的标识符像

attributes.put("width" + someIntIdentifier, image.getAttribute("width"); 
0

您使用的地图和不断更新的关键值,一个键地图只能有一个值(它取代了以前的值)。或者,您可以尝试使用地图列表,其中将包含包含有关图像信息的地图。

String name = ""; 
String width = ""; 
String height = ""; 


List imageNameList = new ArrayList(); // creating list to store imageName 
List imageAttributesList = new ArrayList(); // creating list to store image height and width 


List<WebElement> imageName = driver.findElements(By.cssSelector("div.card-arago div.hover-info div.name")); 
List<HashMap> imageInfo = new ArrayList<HashMap>(); 
imageInfo.add(attributes); 
for (WebElement image : imageName) { 
    HashMap<String, String> attributes = new HashMap<String, String>(); 
    attributes.put("name",image.getText()); 

    imageNameList.add(attributes); // adding map to list 
} 
List<WebElement> images = driver.findElements(By.cssSelector("div.card-arago a img")); 
for (WebElement image : images) { 

    HashMap<String, String> attributes = new HashMap<String, String>(); 

    attributes.put("width", image.getAttribute("width")); 
    attributes.put("height", image.getAttribute("height")); 

    imageAttributesList.add(attributes); // adding map to list 
} 


System.out.println(imageNameList); // this will give you a list of Map having "name" key 
System.out.println(imageAttributesList); // this will give you a list of Map having "height" and "width" of image 

在这里,我已经创建了两个列表存储图像的名称和高度&宽度。像这样你可以获得你需要的所有图像的属性。

0

如果我明白你正在尝试做什么,我同意jthomas返回一个HashMap列表。此外,与其管理和循环使用名称文本的元素列表,然后分别查找和循环图像元素的维度,您应该能够合并一点,如果我假设这两个元素都在公共格内。我会做这样的事情:然后

List<HashMap> images = new ArrayList<HashMap>(); 
List<WebElement> imageDivs = driver.findElements(By.cssSelector("div.card-arago"); 

for (WebElement imageDiv : imageDivs) { 
    HashMap<String, String> attributes = new HashMap<String, String>(); 
    attributes.put("name", imageDiv.findElement(By.cssSelector("div.name").getText(); 

    WebElement image = imageDiv.findElement(By.tagName("img"); 
    attributes.put("width", image.getAttribute("width")); 
    attributes.put("height", image.getAttribute("height")); 
    images.add(attributes); 
} 

的图像列表会给你的页面上的每个图像卡,每个包含名称,高度,以及图像的宽度的HashMap中。然后你可以随心所欲地重复这个列表。