2016-05-13 36 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