【问题标题】:How to store each web elements in a distinct variable whose index is increments in XPath for each web element如何将每个 Web 元素存储在一个不同的变量中,该变量的索引是 XPath 中每个 Web 元素的增量
【发布时间】:2017-12-09 14:10:47
【问题描述】:
   String spath = ".//*[@id='container']/div/div[2]/div/div/div[";

   String epath = "]/div[2]";
   for(int i = 1; i < 5; i++) {

      String tpath = spath+i+epath;
      WebElement listpath = h.findElement(By.xpath(tpath));
      String list = listpath.getText();
      System.out.println(list);

    }

在这种情况下,我已将所有 web 元素 存储在 single 变量“list”中,但我需要存储 每个 distinct 变量中的每个 web 元素

【问题讨论】:

  • 请发布指向该页面或相关 HTML 的链接,并说明目标是什么。

标签: java selenium selenium-webdriver data-structures


【解决方案1】:

这完全取决于您以后将如何访问它。如果顺序不重要,请创建一个集合,它还会通过在顶部添加重复元素:

Set<String> myElements = new HashSet<>();

如果顺序很重要,并且您需要按顺序访问元素,请使用:

List<String> myElements = new ListArray<>();

要在您将使用的集合中添加元素:

myElements.add(list);

而不是打印它。

您可以使您的集合包含任何类型,例如,您可以将 String 替换为 WebElement。在这种情况下,将列表路径添加到您的集合中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 2012-09-06
    相关资源
    最近更新 更多