简体   繁体   English

向列表中添加第二个项目似乎会覆盖我添加的第一个元素

[英]Adding a second item to a list seems to override the first element I added

So I'm trying to add 2 Item objects into a list by looping through it but somehow the 2nd item overrides the first one. 因此,我试图通过遍历列表将2个Item对象添加到列表中,但是以某种方式第二个对象将覆盖第一个。 Sorry it's been a while since I coded so I'm having a hard time figuring out what I did wrong as I expect that the name for these items are different. 抱歉,自从我编码以来已经有一段时间了,所以我很难弄清楚我做错了什么,因为我希望这些项目的名称不同。 :D :d

public static List<Item> buyList = new ArrayList<Item>();
int qty=2;

public static void buyItems(int qty)
{
    for(int i=1; i<=qty; i++)
    {
        Item thisItem = new Item();
        thisItem.setName(driver.findElement("//xpathOfName["+i+"]").getText()));
        thisItem.setPrice(driver.findElement("//xpathOfPrice["+i+"]").getText()));

//      System.out.println("this item is >>>>>>>>" +thisItem.getName());

        buyList.add(thisItem);

//      System.out.println("item at 0 is >>>>>>>>" +buyList.get(0).getName());
    }
}

Before adding "thisItem" into the buyList, it prints the correct name. 在将“ thisItem”添加到buyList之前,它将打印正确的名称。 But the moment I print it outside the loop, it prints the 2nd item twice (the ones I commented out). 但是,当我在循环外打印它时,它会打印第二个项目两次(我注释掉的项目)。 I also tried by directly adding a new item with name and price as parameters but it still seems to overwrite the previously added item into the list. 我还尝试通过直接添加名称和价格为参数的新项目来尝试,但它似乎仍将以前添加的项目覆盖到列表中。

I tried to simplify the html code below: 我试图简化下面的html代码:

<ul>
   <li id="item-1">
      <div>
         <h4>First Item</h4>
      </div>
   </li>
   <li id="item-2">
      <div>
         <h4>Second Item</h4>
      </div>
   </li>
</ul>

As you have induced the loop and you want to access the element through an index , you need to replace the following lines: 引起循环并要通过索引访问元素时,需要替换以下行:

thisItem.setName(driver.findElement("//xpathOfName["+i+"]").getText()));
thisItem.setPrice(driver.findElement("//xpathOfPrice["+i+"]").getText()));

With: 附:

thisItem.setName(driver.findElements("//xpathOfName").get(i).getText()));
thisItem.setPrice(driver.findElements("//xpathOfPrice").get(i).getText()));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在列表中添加每个项目的第二个元素? - How would I add the second element of each item in a list? 将第二个项目添加到我的堆栈窗格时,第一个项目将丢失其Event / MouseOn。 为什么? 我该怎么办? JavaFX的 - When adding a second item to my stackpane, the first item loses its Event/MouseOn. Why? How can I fix? JavaFX 为什么将第一项添加到集合中比第二项慢得多? - why adding first item into collection much slower than second? 在链表Java中首先添加元素时出错 - Error adding element first in linked list java 文件似乎为空,但我可以得到第一个元素 - Document seems to be null but I can get the first element 如何在 java 的嵌套列表中获取每个列表的第一个和第二个元素 - How to get first and second element of a each list in a nested list in java 将 Map 的第一个元素添加到第一个 Position 的另一个列表中,依此类推...第二个 map 元素到第二个 Z4757FE07FD492ADBEEAZ6 列表中 - Add first element of Map into another list at 1st Position and so on…second map element into second position of list 我看不到添加到ListView的第一项 - I can't see the first item added to my ListView 按第一个元素对对象列表进行排序,但如果相等则按第二个元素排序 - Sorting a List of objects by the First element but if Equal sort by the second 我只能访问列表中的第一项 - I can only access first item in the list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM