0

The HTML I'm trying 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">

Clicking the below opens a modal window but I'm struggling to get the click to work,

<td class="col-align-left" role="gridcell"><a href="#" class="cell-popover" data-col-type="popover" data-col-name="ST_COPYCUST">MUFC</a></td>

As the table is dynamic I don't think I can use the XPath, the code at present looks like this,

driver.FindElement(By.ClassName("cell-popover")).Click();

Which works great as long as the cell is on the first row of the table, however this will not always be the case so would like some advice\help on how to proceed?

UPDATE

Solved it with this,

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");
IList<IWebElement> cells = row.FindElements(By.TagName("td"));
for (int i = 0; i < cells.Count; i++)
{
    IWebElement cell = cells[i];
}
row.FindElement(By.ClassName("cell-popover")).Click();

As the td containing "LU1" is always unique, be intersted to know if there is a more efficient\reliable way of doing it though!

Second Update

Refined it slightly to the following,

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

Seems to be reliable

  • either By.LinkText("MUFC") or By.CssSelector("[data-col-name='ST_COPYCUST']") or By.XPath("//a[normalize-space()='MUFC']"). You may need to use a waiter if the content is created dynamically. – Florent B. Mar 15 '18 at 18:48
  • The problem with those approaches is that the LinkText "MUFC" for that column will not be unique so i need to specify the row to find the LinkText in - I was thinking of trying to use the <tr data-uid="23f8ebd0-2c90-4728-9430-71376f2d10e3" to identify the row but I'm not sure the best way to go about it. – Luke Mar 15 '18 at 23:37
  • What is the exact error you are getting in other dynamic cases? – Vishal Aggarwal Mar 16 '18 at 7:35
  • As I understand , there is only one link going to be in any case in the table (only row can differ),if yes then object is unique so what is the issue here? – Vishal Aggarwal Mar 16 '18 at 7:37
  • Happy to close this as my implementation seems reliable, thanks for the comments and answers!! – Luke Mar 16 '18 at 19:06
0

Why not simply try the below,

By.Css('div[id^="enquiry-header-grid-SalesTransactionEnquiry"] table[role="grid"] a[class="cell-popover"]')
0

If I'm understanding this right, this is criteria we can count on?:

  • The row (tr) in table that you want to click a cell in contains the text "LU1"
  • the cell (td) in row will have a class = cell-popover

Then this XPath may work:

var elemTable = driver.FindElement(By.Id("enquiry-header-grid-SalesTransactionEnquiry-enquiry-header-grid-wrapper"));
var popoverCell = elemTable.FindElement(By.XPath(".//tr[contains(text(),'LU1')]/td[@class='cell-popover']"));

This should find the first tr that contains text with "LU1" (in the elemTable), then give you the first child td in that tr that has class of "cell-popover".

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.