2016-05-13 40 views
0

我有一些頁面上的3列,並有一個按日期排序頁面上的排序按鈕。我一直在努力弄明白,沒有運氣。以下是一條記錄的例子。一頁上可能有50個。排序硒對象C# - 不是表

如何將所有三個「列」都放到一個數組中並按asc或desc順序對輸出進行排序?然後,我可以單擊排序值並獲取新的數組列表並進行比較。如果我做了一個findElements * .toList,它會給我一個所有行的列表,但我無法識別日期字段以進行排序。

我在想什麼?

 |*444577233 444577233 (WORK 555.123.4567)* | *Voicemail (1 min 49 sec)* | *Apr 09, 2016 09:42 AM*| 

     <div class="full-mailbox-message alert alert-warning" style="display: none;"> 
     <div id="message-list" class="message-list message-list-scroll"> 
     <div class="list-item filterable message-item js-message-item cp " data-id="9408"> 
     <span class="inline"> 
     <input class="custom js-message-item-checkbox" type="checkbox"> 
     <label></label> 
     </span> 
     <span class=" " data-id="9408"> 
     <i class="message-list-icon icon-voice-mail custom-icon"></i> 
     <span class="sender" data-phone="5555551759" title="CONPORT 010,CR3522">CONPORT 010,CR3522 (555.555.1759)</span> 
     <span class="time" title="Apr 08, 2016 07:30 PM">Apr 08, 2016 07:30 PM</span> 
     <span class="summary"> 
     <span class="text"> Voicemail (57 sec) </span> 
     </span> 
     </span> 
     </div> 
     <div class="list-item filterable message-item js-message-item cp " data-id="8592"> 
     <span class="inline"> 
     <input class="custom js-message-item-checkbox" type="checkbox"> 
     <label></label> 
     </span> 
     <span class=" " data-id="8592"> 
     <i class="message-list-icon icon-voice-mail custom-icon"></i> 
     <span class="sender" data-phone="5555551759" title="CONPORT 010,CR3522">CONPORT 010,CR3522 (555.555.1759)</span> 
     <span class="time" title="Mar 28, 2016 08:31 PM">Mar 28, 2016 08:31 PM</span> 
     <span class="summary"> 
     <span class="text"> Voicemail (7 sec) </span> 
     </span> 
     </span> 
     </div> 
     <div class="list-item filterable message-item js-message-item cp " data-id="8523"> 
     <span class="inline"> 
     <input class="custom js-message-item-checkbox" type="checkbox"> 
     <label></label> 
     </span> 
     <span class=" " data-id="8523"> 
     <i class="message-list-icon icon-voice-mail custom-icon"></i> 
     <span class="sender" data-phone="5555551754" title="CONPORT 005,CR3522">CONPORT 005,CR3522 (555.555.1754)</span> 
     <span class="time" title="Mar 27, 2016 11:45 PM">Mar 27, 2016 11:45 PM</span> 
     <span class="summary"> 
     <span class="text"> Voicemail (7 sec) </span> 
     </span> 
     </span> 
     </div> 
     <div class="list-item filterable message-item js-message-item cp " data-id="8477"> 
     <span class="inline"> 
     <input class="custom js-message-item-checkbox" type="checkbox"> 
     <label></label> 
     </span> 
     <span class=" " data-id="8477"> 
     <i class="message-list-icon icon-voice-mail custom-icon"></i> 
     <span class="sender" data-phone="withheld" title="Identity withheld">Identity withheld (withheld)</span> 
     <span class="time" title="Mar 24, 2016 03:51 AM">Mar 24, 2016 03:51 AM</span> 
     <span class="summary"> 
     <span class="text"> Voicemail (1 sec) </span> 
     </span> 
     </span> 
     </div> 
     <div class="list-item filterable message-item js-message-item cp " data-id="8470"> 
     <span class="inline"> 
     <input class="custom js-message-item-checkbox" type="checkbox"> 
     <label></label> 
     </span> 
     <span class=" " data-id="8470"> 
     <i class="message-list-icon icon-voice-mail custom-icon"></i> 
     <span class="sender" data-phone="withheld" title="Identity withheld">Identity withheld (withheld)</span> 
     <span class="time" title="Mar 22, 2016 09:59 PM">Mar 22, 2016 09:59 PM</span> 
     <span class="summary"> 
     <span class="text"> Voicemail (4 sec) </span> 
     </span> 
     </span> 
     </div> 
     <div class="list-item filterable message-item js-message-item cp " data-id="7191"> 
     <span class="inline"> 
     <input class="custom js-message-item-checkbox" type="checkbox"> 
     <label></label> 
     </span> 
     <span class=" " data-id="7191"> 
     <i class="message-list-icon icon-voice-mail custom-icon"></i> 
     <span class="sender" data-phone="withheld" title="Identity withheld">Identity withheld (withheld)</span> 
     <span class="time" title="Mar 04, 2016 04:53 AM">Mar 04, 2016 04:53 AM</span> 
     <span class="summary"> 
     <span class="text"> Voicemail (4 sec) </span> 
     </span> 
     </span> 
     </div> 
     <div class="list-item filterable message-item message-unread js-message-item cp " data-id="7074"> 
     <span class="inline"> 
     <input class="custom js-message-item-checkbox" type="checkbox"> 
     <label></label> 
     </span> 
     <span class=" " data-id="7074"> 
     <i class="message-list-icon icon-voice-mail custom-icon"></i> 
     <span class="sender" data-phone="5555551756" title="cert 1756">cert 1756 (555.555.1756)</span> 
     <span class="time" title="Mar 03, 2016 09:03 AM">Mar 03, 2016 09:03 AM</span> 
     <span class="summary"> 
     <span class="text"> Voicemail (4 sec) </span> 
     </span> 
     </span> 
     </div> 

回答

1

我想你必須使用DataTable和DataView。第一步:將所有行逐個添加到DataTable。然後創建DataView並按字段排序。然後單擊排序並創建一個新的DataTable實例並再次添加所有行/單元格。最後,比較每行DataView和DataTable中的數據。 請參閱下面的代碼:

public void TestSorting() 
{ 
    DataTable table1 = GenerateTable(); 
    DataView dv = table1.DefaultView; 
    dv.Sort = "datetime ASC"; // or "datetime DESC" 
    DataRowCollection sortedRows = dv.ToTable().Rows; 

    // Click sort here e.g. timeHeader.Click(); 

    DataTable table2 = GenerateTable(); 
    DataRowCollection rows = table1.Rows; 

    // Compare sortedRows and rows here 
} 

public DataTable GenerateTable() 
{ 
    DataTable table = new DataTable(); 
    table.Columns.Add("datetime", typeof(DateTime)); 
    table.Columns.Add("sender", typeof(string)); 
    table.Columns.Add("time", typeof(string)); 
    table.Columns.Add("summary", typeof(string)); 

    IReadOnlyCollection<IWebElement> rows = driver.FindElements(By.CssSelector("#message-list > div.list-item ")); 
    foreach (var listItem in rows) 
    { 
     IWebElement sender = listItem.FindElement(By.CssSelector("span.sender")); 
     IWebElement time = listItem.FindElement(By.CssSelector("span.time")); 
     IWebElement summary = listItem.FindElement(By.CssSelector("span.summary")); 

     DataRow r = table.NewRow(); 

     string format = "MMM dd, yyyy hh:mm tt"; 
     DateTime dt = DateTime.ParseExact(time.Text.Trim(), format, CultureInfo.InvariantCulture); 
     r["datetime"] = dt; 
     r["sender"] = sender.Text; 
     r["time"] = time.Text; 
     r["summary"] = summary.Text; 

     table.Rows.Add(r); 
    } 

    return table; 
} 
+0

感謝Buaban。當我回家時,我會在今晚看看這個。我玩了幾分鐘,即使我只有3行,sortRows和rows在計數中返回113,它看起來像在項目數組中的重複很多,我不知道爲什麼。在行和排序行上運行AreEqual失敗。對幫助非常感謝。我從來沒有見過這個過程,它絕對是很酷的。 – Dazed

+0

@Dazed你可以發佈完整的HTML源代碼嗎?所以我可以找出什麼應該是行定位器。 – Buaban

+0

當我今晚回到家時,我會添加它。謝謝您的幫助。 – Dazed