This is a migrated thread and some comments may be shown as answers.

How to search an item in a htmltable?

23 Answers 794 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Sudarshan
Top achievements
Rank 2
Sudarshan asked on 12 Mar 2013, 04:10 AM
hi,

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

Thanks
Sudarshan

23 Answers, 1 is accepted

Sort by
0
Boyan Boev
Telerik team
answered on 12 Mar 2013, 08:39 AM
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
0
Sudarshan
Top achievements
Rank 2
answered on 12 Mar 2013, 09:09 AM
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
0
Boyan Boev
Telerik team
answered on 12 Mar 2013, 11:33 AM
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
0
Billa
Top achievements
Rank 1
answered on 17 Jul 2013, 06:24 AM
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 ?
0
Sudarshan
Top achievements
Rank 2
answered on 17 Jul 2013, 01:48 PM
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
0
Boyan Boev
Telerik team
answered on 18 Jul 2013, 11:14 AM
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
0
Suresh
Top achievements
Rank 1
answered on 11 Oct 2013, 06:39 AM
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
0
Boyan Boev
Telerik team
answered on 11 Oct 2013, 07:45 AM
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
0
Suresh
Top achievements
Rank 1
answered on 17 Oct 2013, 01:46 PM
Thanks Boyan... It's working
0
Suresh
Top achievements
Rank 1
answered on 18 Oct 2013, 05:50 AM
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.
0
Daniel
Top achievements
Rank 2
answered on 18 Oct 2013, 12:06 PM
Are you trying to get the anchor property or simply the text of the anchor?  Does the anchor have a value or name?
0
Velin Koychev
Telerik team
answered on 22 Oct 2013, 12:21 PM
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
0
Suresh
Top achievements
Rank 1
answered on 11 Nov 2013, 05:23 AM
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
0
Velin Koychev
Telerik team
answered on 13 Nov 2013, 11:41 AM
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
0
Suresh
Top achievements
Rank 1
answered on 19 Nov 2013, 10:24 AM
Thanx a lot Velin,
It's working fine with very minor modification.
0
Viral
Top achievements
Rank 1
answered on 21 Sep 2014, 03:12 AM
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?
0
Boyan Boev
Telerik team
answered on 24 Sep 2014, 01:38 PM
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
 
0
sherjo
Top achievements
Rank 1
answered on 26 Feb 2015, 10:28 AM
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!


0
Boyan Boev
Telerik team
answered on 03 Mar 2015, 07:52 AM
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
 
0
sherjo
Top achievements
Rank 1
answered on 03 Mar 2015, 02:34 PM
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!
0
Boyan Boev
Telerik team
answered on 03 Mar 2015, 02:46 PM
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
 
0
Swapna
Top achievements
Rank 1
answered on 19 Oct 2015, 05:50 PM

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!

0
Boyan Boev
Telerik team
answered on 22 Oct 2015, 06:32 AM
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
 
Tags
General Discussions
Asked by
Sudarshan
Top achievements
Rank 2
Answers by
Boyan Boev
Telerik team
Sudarshan
Top achievements
Rank 2
Billa
Top achievements
Rank 1
Suresh
Top achievements
Rank 1
Daniel
Top achievements
Rank 2
Velin Koychev
Telerik team
Viral
Top achievements
Rank 1
sherjo
Top achievements
Rank 1
Swapna
Top achievements
Rank 1
Share this question
or