簡體   English   中英

將WebElement屬性添加到字符串數組

[英]Adding WebElement attributes to a string array

我希望將日歷中的日期與另一個地方進行比較,以驗證日期是否出現在用戶選擇日期之后的其他位置。 我正在使用這些日期並將其轉換為另一種格式以進行驗證。

我遇到的問題是從包含日期信息的web元素屬性創建數組時。 我嘗試遍歷所選日期,然后采用屬性,將其轉換為我的格式,然后將其添加到列表中,以供日后查看。

每次查看字符串列表時,它都是空的。 我認為僅使用.add()即可,但事實並非如此。 即使當我打印一個靜態字符串時,我也硬編碼來驗證collectdDateStrings,但在控制台上卻什么也沒得到。

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')]"));

我找到了答案。 上面的代碼結構還可以,但是列出的元素比我意識到的要多。 'lblDate'應該有4個元素,但有7個。前三個元素為null,因此我按如下所示刪除了它們:

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);

現在,我正在尋找適合我的列表的元素。 再次感謝您的答復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM