2

I'm experiencing a few issues with trying to create a list using List<IWebElement>from a local variable.

The HTML I'm working with is below,

<div id="enquiry-header-grid-SalesTransactionEnquiry-enquiry-header-grid-wrapper" class="enquiry-kendo-grid-wrapper k-grid k-widget" data-role="grid" style="height: 456px;">
 <div class="k-grid-content k-auto-scrollable" style="height: 428px;">
  <table role="grid" tabindex="0" data-role="selectable" class="k-selectable" style="touch-action: none;">
   <colgroup><col style="width:120px"><col style="width:120px"><col style="width:100px"><col style="width:60px"><col style="width:100px"><col style="width:105px"><col style="width:100px"><col style="width:120px"><col style="width:120px"><col style="width:100px"><col style="width:200px"><col style="width:120px"><col style="width:120px"><col style="width:60px"><col style="width:100px"><col style="width:100px"><col style="width:100px"><col style="width:100px"><col style="width:100px"><col style="width:120px"><col style="width:60px"><col style="width:100px"><col style="width:100px">    </colgroup>
   <tbody role="rowgroup">
    <tr data-uid="23f8ebd0-2c90-4728-9430-71376f2d10e3" role="row" class="k-state-selected" aria-selected="true">
     <td class="col-align-left" role="gridcell"><a href="#" class="cell-popover" data-col-type="popover" data-col-name="ST_COPYCUST">MUFC</a></td>
     <td class="col-align-left" role="gridcell">Manchester United</td>
     <td class="col-align-right" role="gridcell">23/10/2017</td>
     <td class="col-align-right" role="gridcell">1</td>
     <td class="col-align-left" role="gridcell">Invoice</td>
     <td class="col-align-left" role="gridcell">1000</td>
     <td class="col-align-center" role="gridcell"><input type="checkbox" disabled="disabled"></td>
     <td class="col-align-right" role="gridcell">5.00</td>
     <td class="col-align-right" role="gridcell">1.00</td>
     <td class="col-align-left" role="gridcell">SA</td>
     <td class="col-align-left" role="gridcell"></td>
     <td class="col-align-right" role="gridcell">6.00</td>
     <td class="col-align-left" role="gridcell">0.00</td>
     <td class="col-align-center" role="gridcell"><input type="checkbox" disabled="disabled"></td>
     <td class="col-align-left" role="gridcell">2017</td>
     <td class="col-align-left" role="gridcell">1</td>
     <td class="col-align-right" role="gridcell">23/10/2017</td>
     <td class="col-align-right" role="gridcell">23/10/2017</td>
     <td class="col-align-left" role="gridcell"></td>
     <td class="col-align-left" role="gridcell">0.00</td>
     <td class="col-align-center" role="gridcell"><input type="checkbox" disabled="disabled"></td>
     <td class="col-align-left" role="gridcell"></td><td class="col-align-left" role="gridcell"></td>
    </tr>
    <tr class="k-alt" data-uid="5a1d2279-5588-4c41-ab22-ce5361192a4c" role="row">
     <td class="col-align-left" role="gridcell"><a href="#" class="cell-popover" data-col-type="popover" data-col-name="ST_COPYCUST">MUFC</a></td>

The issue is that intermittently the list is created with no values so when i try to access a value in the list it doesn't exist - i have got round this by using Thread.sleep between creating the variable and creating the list but dont really want to rely on this so any help with a better solution would be great.

Non working code

    IWebElement elemTable = driver.FindElement(By.Id("enquiry-header-grid-SalesTransactionEnquiry-enquiry-header-grid-wrapper")).FindElement(By.TagName("tbody"));
    List<IWebElement> lstTrElem = new List<IWebElement>(elemTable.FindElements(By.TagName("tr")));
    IWebElement row = lstTrElem.Where(item => item.Text.Contains("LU1")).FirstOrDefault();
    string rowid = row.GetAttribute("data-uid");
    row.FindElement(By.ClassName("cell-popover")).Click();

Working code with Thread.Sleep

    IWebElement elemTable = driver.FindElement(By.Id("enquiry-header-grid-SalesTransactionEnquiry-enquiry-header-grid-wrapper")).FindElement(By.TagName("tbody"));
    Thread.Sleep(1000);
    List<IWebElement> lstTrElem = new List<IWebElement>(elemTable.FindElements(By.TagName("tr")));
    IWebElement row = lstTrElem.Where(item => item.Text.Contains("LU1")).FirstOrDefault();
    string rowid = row.GetAttribute("data-uid");
    row.FindElement(By.ClassName("cell-popover")).Click();
2

Using WebDriverWait is almost always preferred to Thread.Sleep

private WebDriverWait wait;
wait.Until(ExpectedConditions.ElementExists(By.TagName("tr"))); // or your equivalent By
  • Thanks for the response, im using expected condition for wait commands in most instances I just wasn't sure it would work in this context as I am waiting for a variable to be populated rather than an element?? – Luke Mar 16 '18 at 18:57
  • Well, you can look at as your variable is an element as well, right? You could use the expected condition with the By of the table containing the rows you wish to populate in your List, before setting the table variable, for example. – Subjective Reality Mar 16 '18 at 19:09
  • Hmm interesting! Hadn't thought of it like that, sounds like a decent approach. Il give it a go – Luke Mar 16 '18 at 19:49
  • Bingo - thanks to @Subjective Reality used wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.TagName("tr"))); which now seems to be working very well! – Luke Mar 16 '18 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.