将WebElement属性添加到字符串数组

时间:2016-06-30 15:28:01

标签: java arrays string selenium element

我希望比较日历到其他地方的日期,以验证用户选择日期之后的日期。我正在考虑这些日期,并将它们转换为另一种格式进行验证。

我遇到的问题是从包含日期信息的web elements属性创建数组时。我试图迭代选定的日期,然后获取属性,将其转换为我的格式,然后将其添加到列表中以便稍后查看。

每次查看字符串列表时,它都是空的。我想只是使用.add()可以工作,但事实并非如此。即使我打印一个静态字符串我硬编码来验证collectDateStrings,我在控制台上什么都没有。

List < String > collectedDateStrings = new ArrayList < String > ();

//Find all dates that are currently selected.  Create a list of these to use later.
List < WebElement > selectedDates = driver.findElements(highlightedDates);
for (WebElement we: selectedDates) {

    collectedDateStrings.add(Utils.convertDateString(we.getAttribute("title")));
}

for (String s: collectedDateStrings) {
    System.out.println("this value is: " + s);
} //this yields nothing

修改 根据一些评论我添加了这段代码: 这是我试图从中获取日期的元素:

<div class="cvDialogRow" style="width: 149px; text-align: left;"><label id="lblDate0">Wed 06/29/2016<br></label></div

这是我用来查找元素和列表的命令:

List<WebElement> occurences = driver.findElement(By.xpath("//div[starts-with(@class, 'cvDialogRow')]")).findElements(By.xpath("//label[starts-with(@id, 'lblDate')]"));

1 个答案:

答案 0 :(得分:0)

我找到了答案。上面的代码结构还可以,但我看到的元素列出的元素比我意识到的要多。 &#39; lblDate&#39;应该有4个元素。但是有7个。前3个是空的所以我只是删除它们如下:

List<WebElement> occurences = driver.findElement(By.xpath("//div[starts-with(@class, 'cvDialogRow')]")).findElements(By.xpath("//label[starts-with(@id, 'lblDate')]"));
    occurences.remove(0);
    occurences.remove(0);
    occurences.remove(0);

现在我正在查看列表中的正确元素。再次感谢您的回复。