How to search an item in a htmltable?

24 posts, 0 answers
  1. Sudarshan
    Sudarshan avatar
    22 posts
    Member since:
    Feb 2013

    Posted 11 Mar 2013 Link to this post

    hi,

    My application has a table, i have to search for an item in a table. How to do that?

    Thanks
    Sudarshan
  2. Boyan Boev
    Admin
    Boyan Boev avatar
    1045 posts

    Posted 12 Mar 2013 Link to this post

    Hello Sudarshan,

    You can achieve this via simple find expressions. Read more about them here.

    See below for complete sample code against an html table:

    HTML:

    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    <table border="1" id="myTable">
    <tr>
    <th>Header 1</th>
    <th>Header 2</th>
    </tr>
    <tr>
    <td>row 1, cell 1</td>
    <td><a href="http://www.google.com">Google</a></td>
    </tr>
    <tr>
    <td>row 2, cell 1</td>
    <td>row 2, cell 2</td>
    </tr>
    </table>
    </body>
    </html>

    Find expressions (C#):

    HtmlTable table =
    Manager.ActiveBrowser.Find.ByAttributes<HtmlTable>(
    "id=myTable");
    HtmlAnchor googleLink = table.Find.ByAttributes<HtmlAnchor("href=http://www.google.com");
    googleLink.Click();


    This will locate the table with ID="myTable" then it will find and click on a link with href="http://www.google.com".

    I recorded a short video as a demonstration.

    Hope this helps.

    Greetings,
    Boyan Boev
    the Telerik team
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
  3. Sudarshan
    Sudarshan avatar
    22 posts
    Member since:
    Feb 2013

    Posted 12 Mar 2013 Link to this post

    Hi,

    I dont want to find the element by href, i want to search it using text content or inner text. Coz href is not static in this case, href keeps changing if any changes are made to the element.

    I referred this link but couldnt get much help.
    http://www.telerik.com/automated-testing-tools/support/documentation/user-guide/code-samples/html/table-search-and-paging.aspx

    Thanks
    Sudarshan
  4. Boyan Boev
    Admin
    Boyan Boev avatar
    1045 posts

    Posted 12 Mar 2013 Link to this post

    Hi Sudarshan,

    Please see bellow the two examples, that are using text content.

    HtmlTable table = Manager.ActiveBrowser.Find.ByAttributes<HtmlTable>("id=myTable");
    HtmlAnchor googleLink = table.Find.ByContent<HtmlAnchor>("Google");
    googleLink.Click();

    HtmlTable table = Manager.ActiveBrowser.Find.ByAttributes<HtmlTable>("id=myTable");
    HtmlAnchor googleLink = table.Find.ByExpression<HtmlAnchor>("textcontent=Google");
    googleLink.Click();


    Here is another video which demonstrates it.

    Regards,

    Boyan Boev
    the Telerik team
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
  5. Billa
    Billa avatar
    1 posts
    Member since:
    Jul 2013

    Posted 17 Jul 2013 Link to this post

    Hi Boyan,
    In My Case, i need to click delete or edit button of the dynamic name in the HTML table list using data driven concept. how to do that using coded steps in test studio ? can you provide me with example ?
  6. Sudarshan
    Sudarshan avatar
    22 posts
    Member since:
    Feb 2013

    Posted 17 Jul 2013 Link to this post

    Hi Billa,

    Once you find the table, you can use these to find the element if the name or any other property is dynamic.

    HtmlTable table = manager.Activebrowser.Find.ByAttributes<HtmlTable>("table property"); //Find the table

    HtmlControl delete/edit = table.Find.AllByTagIndex<HtmlControl>("tagname", tagindex);
    or
    var delete/edit = table.Find.AllByTagName<HtmlControl>("tag name of that element");
    This will return collection of elements with the tag name u mention and then you can use the index of that element to access that.
    Ex: delete/edit[index no].Click(); 

    Thanks
    Sudarshan
  7. Boyan Boev
    Admin
    Boyan Boev avatar
    1045 posts

    Posted 18 Jul 2013 Link to this post

    Hi,

    @Sudarshan - thank you for you input, I've updated your Telerik points.

    @Billa - Did this help? Are you still experiencing the issue?

    Should you have any additional questions please do not hesitate to ask us.

    Regards,
    Boyan Boev
    Telerik
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
  8. Suresh
    Suresh avatar
    5 posts
    Member since:
    Jul 2013

    Posted 11 Oct 2013 Link to this post

    I have a simple HTML table in my application, where I'm searching a particular row by using the expression "innertext=mytext" which is working fine. I just want to make this search as parameter search from data sheet. Please let me know how to pass the data from binded data sheet to ("innertext=xxxxxxx")
    Example: 
    HtmlTableRow myRow = table.Find.ByExpression<HtmlTableRow>("Innertext=abc01");
    HtmlTableRow myRow = table.Find.ByExpression<HtmlTableRow>("Innertext=abc02");
    HtmlTableRow myRow = table.Find.ByExpression<HtmlTableRow>("Innertext=abc03");

    Please let me know if you want any other details

    Regards,
    - Suresh
  9. Boyan Boev
    Admin
    Boyan Boev avatar
    1045 posts

    Posted 11 Oct 2013 Link to this post

    Hello Suresh,

    Here is a sample code:

    HtmlTableRow myRow = table.Find.ByExpression<HtmlTableRow>("Innertext=" + Data["nameOfTheColumn"].ToString());
     
    Hope this helps.

    Regards,
    Boyan Boev
    Telerik
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
  10. Suresh
    Suresh avatar
    5 posts
    Member since:
    Jul 2013

    Posted 17 Oct 2013 Link to this post

    Thanks Boyan... It's working
  11. Suresh
    Suresh avatar
    5 posts
    Member since:
    Jul 2013

    Posted 18 Oct 2013 Link to this post

    I have another issue of same kind where I'm searching a row in an html table by "innertext=Name01"
    The first column in html table contains distinct names which is of type hyperlink.
    Example
    Column1
    <a>Name01</a>
    <a>Name01</a>
    ....
    <a>Name99</a>

    I'm using the  below code to search a particular row with the text  under column1 & it's not working.
    HtmlTableRow row = table.Find.ByExpression<HtmlTableRow>("innertext=Name01");

    Please help me in resolving this issue.
  12. Daniel
    Daniel avatar
    13 posts
    Member since:
    Aug 2013

    Posted 18 Oct 2013 Link to this post

    Are you trying to get the anchor property or simply the text of the anchor?  Does the anchor have a value or name?
  13. Velin Koychev
    Admin
    Velin Koychev avatar
    88 posts

    Posted 22 Oct 2013 Link to this post

    Hi Suresh,

    This code will work only if you have one column. As Daniel asked, could you please describe better your testing scenario, so we could help you better. It would also help us if you provide us with a copy of your test and if it is possible grant us access to your application so we can reproduce the issue on our end and give you a solution.

    Hope to hear from you soon.

    Regards,
    Velin Koychev
    Telerik
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
  14. Suresh
    Suresh avatar
    5 posts
    Member since:
    Jul 2013

    Posted 10 Nov 2013 Link to this post

    Hi Velin,
    I have attached a screenshot of sample HTML table which is same as my HTML table.
    In the table the data under first column (Column name = CustomerID) is unique and it's hyperlinked.

    My Issue:
    I want to search a particular CustomerId from first column by it's name & click on it.

    Example: I want to search the CustomerID = "KASHM" by name = "KASHM" & click on it.

    Please help me in getting this resolved.

    Regards,
    Suresh
  15. Velin Koychev
    Admin
    Velin Koychev avatar
    88 posts

    Posted 13 Nov 2013 Link to this post

    Hello Suresh,

    Thank you for the clarifications.

    Here is a sample code that executes the same test scenario:

    //Find the table on the site.
    HtmlTable table = ActiveBrowser.Find.ById<HtmlTable>("myTable");
    //Get all rows of the table.
    IList<HtmlTableRow> list = table.Find.AllByTagName<HtmlTableRow>("tr");
    string name = "KASHM";
    //Get the values of the first column.
    foreach (HtmlTableRow item in list)
    {
        //locate the cell from the current row that is in the first column
        HtmlTableCell currentCell = item.Cells[0];
        //get the link that is inside this cell
        HtmlAnchor customerID = currentCell.Find.ByTagIndex<HtmlAnchor>("a", 0);
         
        //if the text content of this link is the same as the name, click on the hyperlink
        if (name == customerID.TextContent)
        {
            customerID.Click();
            break;
        }
    }


    Let me know if this helps.

    Regards,
    Velin Koychev
    Telerik
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
  16. Suresh
    Suresh avatar
    5 posts
    Member since:
    Jul 2013

    Posted 19 Nov 2013 Link to this post

    Thanx a lot Velin,
    It's working fine with very minor modification.
  17. Viral
    Viral avatar
    1 posts
    Member since:
    May 2014

    Posted 20 Sep 2014 Link to this post

    Hi,

    I have slightly different scenario. I am evaluating Telerik Test Studio for test automation. 
    I have Html Table something like:

    01.<div class="tab-content">
    02.    <div id="divIDname" class="tab-pane active" >
    03.         
    04.    <table>
    05.        <tr>
    06.            <th>Head1</th>
    07.            <th>Head2</th>
    08.            <th>Head3</th>
    09.        </tr>
    10.         
    11.        <tr>
    12.            <td><a href="link">111111</a></td>
    13.            <td><a href="link">1111</a><br/>22222</td>
    14.            <td>$434343</td>
    15.        </tr>
    16.    </div>
    17.</div>

    I need to click on the first cell of the table data row. I can't use the text because it may change in future. I need to find "111111" (line no.12) element by path. Could you please tell me how can I use XPath or any other Html find expression to find that element in coded step?
  18. Boyan Boev
    Admin
    Boyan Boev avatar
    1045 posts

    Posted 24 Sep 2014 Link to this post

    Hi Viral,

    Here is how you can locate directly the first table cell (td) in the given div:

    HtmlTableCell cell = ActiveBrowser.Find.ByExpression<HtmlTableCell>("id=divIDname", "|", "tagIndex=td:0");

    Here is another approach. It firstly locates the second row of the table and then the first cell:

    //Find the div with id "divIDname"
    HtmlDiv myDiv = ActiveBrowser.Find.ById<HtmlDiv>("divIDname");
    //Find the table in the div
    HtmlTable myTable = myDiv.Find.ByExpression<HtmlTable>("tagname=table");
    //Find the second row
    HtmlTableRow row = myTable.Rows[1];
    //Find the first cell of the second row
    HtmlTableCell cell = row.Cells[0];

    Let me know if this helps.

    Regards,
    Boyan Boev
    Telerik
     
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
     
  19. sherjo
    sherjo avatar
    6 posts
    Member since:
    Feb 2015

    Posted 26 Feb 2015 Link to this post

    Hi, 

    I used the code below, but it only returned values from the rows inside the <thead></thead> tags.
    How do I access the rows inside the <tbody></tbody> tags?

    HtmlDiv tDiv = Find.ByAttributes<HtmlDiv>("class=inner");
    HtmlTable tTbl = tDiv.Find.ById<HtmlTable>("alerttemplate-list");
    IList<HtmlTableRow> list = tTbl.Find.AllByTagName<HtmlTableRow>("tr");
    foreach (HtmlTableRow item in list)
    {
        Log.WriteLine(item.InnerText.ToString());    
    }

    thanks!


  20. Boyan Boev
    Admin
    Boyan Boev avatar
    1045 posts

    Posted 03 Mar 2015 Link to this post

    Hi Sherjo,

    Your code seems correct.

    Could you please send us the DOM of the table so I can assist you with the code.

    Hope to hear from you soon.

    Regards,
    Boyan Boev
    Telerik
     
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
     
  21. sherjo
    sherjo avatar
    6 posts
    Member since:
    Feb 2015

    Posted 03 Mar 2015 in reply to Boyan Boev Link to this post

    Hi Boyan,

    I was able to make my code work!
    I just added, ActiveBrowser.RefreshDomTree(); at the top of my code and the row inside the tbody tags were recognized.

    thanks!
  22. Boyan Boev
    Admin
    Boyan Boev avatar
    1045 posts

    Posted 03 Mar 2015 Link to this post

    Hi Sherjo,

    I am happy to hear that.

    If you need further assistance please let us know.

    Regards,
    Boyan Boev
    Telerik
     
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
     
  23. Swapna
    Swapna avatar
    1 posts
    Member since:
    Sep 2014

    Posted 19 Oct 2015 in reply to Velin Koychev Link to this post

    Hi,

    I am trying to automate the similar type of scenario. Application do have a page with HTML table. Each row displays the customer information. First three columns in the table represent Last Name, First Name and Middle Name. Name field in allof these three columns is a hyperlink and on clicking the link user is navigated to another page.

    Currently, I am facing issues with the code which performs the task of - Searching the page with the Customer name provided via local data (attached for reference)

     When I run the test, below error is displayed even though I have added proper assembly references.

    "The type or namespace name 'UI' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)"

    Thanks!

  24. Boyan Boev
    Admin
    Boyan Boev avatar
    1045 posts

    Posted 22 Oct 2015 Link to this post

    Hello Swapna,

    This is a duplicate of your other thread.

    Please let's continue the discussion there.

    Thank you!

    Regards,
    Boyan Boev
    Telerik
     
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
     
Back to Top