Ask a Question

Need help in counting the total no. of elements in a table

nitinn
Occasional Contributor

Need help in counting the total no. of elements in a table

Hi,

I am trying to count the no. of elements present in a table and if they are present, I need to click on them.

Suppose 3 elements are present, I need to click on each one of them.

Below is my code:

 

 

function findata()
{
//if (page.FindChildByXPath("//table[contains(@id,'repeaterTable')]//tr//td/a[@class = 'textlink hand']")
let arr = page.EvaluateXPath("//table[contains(@id,'repeaterTable')]//tr//td/a[@class = 'textlink hand']");
if (!strictEqual(arr,null))
{
for (i in arr)
{arr[i].Click();
page.FindChildByXPath("//input[contains(@id,'buttonListReturn')]").Click();
Delay(2000)
i++;
}
}
else
{
Log.Error("Nothing found");
}
}

7 REPLIES 7
baxatob
Community Hero

Re: Need help in counting the total no. of elements in a table

Hi,

 

Looks nice. Did you face any problem running your code?

 

While finding elements by XPath is relatively slow method, you can also try FindAll () method >>

nitinn
Occasional Contributor

Re: Need help in counting the total no. of elements in a table

I couldn't count the no. of elements. The For loop isn't navigating.Can you find any error in the code?

baxatob
Community Hero

Re: Need help in counting the total no. of elements in a table

Are you sure that xpath is correct? 

 

Check the size of array using Log.Message(arr.length); 

baxatob
Community Hero

Re: Need help in counting the total no. of elements in a table

 

Also be careful with strictEqual() function:

 

let arr = new Array();
strictEqual(arr, null)); // returns false
strictEqual(arr.length, 0)); // returns true
    

 

nitinn
Occasional Contributor

Re: Need help in counting the total no. of elements in a table

It is working for the first time.Post that its failing.

My guess is its not looping correctly.I used arr.length & its correctly returning me 4 rows for the table.

The error is below:

 

 

Unable to obtain the item's rectangle.

AlexKaras
Community Hero

Re: Need help in counting the total no. of elements in a table

Hi,

 

While it is not clear what happens in your application after the first click, the most probable guess is that the following sequence occurs:

-- Link is clicked;

-- Application navigates to somewhere *and the initial table is no more present on the screen*;

-- Back button/link is clicked;

-- Application navigates back to the initial page *and the table is recreated a-new*

 

If my guess is correct, then all found references to table cells will be invalidated after navigating away from the initial page and you *must* search for them again after you back to the page with the table. Then you should be able to click the second cell and so on.

 

P.S. And yes, as it already has been said, search by XPath is the worst approach in TestComplete. (Especially, when searching through the whole page, like a lot of people used to do. Or, maybe, because of some Selenium specifics?)

 

P.P.S. Use of Aliases might solve the problem with recreated page elements, but this will work only if the given element has its Alias in NameMapping which is not a case with tables (due to how tables are usually designed nowadays).

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
tristaanogre
Community Hero

Re: Need help in counting the total no. of elements in a table

I'm with @AlexKaras.  When you navigate away from the page after clicking on the first item, all handles and references to the previous page are no longer valid.  Your Find returned a bunch of objects with specific handles... and they no longer exist hence your error. What you should do is, within your for loop, refresh your array of items with another find.  The loop will still go on to the next item in the loop but your array will have the correct handles again.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
cancel
Showing results for 
Search instead for 
Did you mean: