Clicking a Specified Row in HTML Table

Ask general questions here.
mcs
Posts: 35
Joined: Mon Aug 11, 2014 5:00 pm
Location: Portsmouth, NH

Clicking a Specified Row in HTML Table

Post by mcs » Tue Apr 05, 2022 10:11 pm

Hello,

I'm attempting to click on a specific row of an HTML table in my user code determined by a randomly generated int.

I've attached a snapshot of my table which is focused on a cell, one item further than the one I'm attempting. In my repository, I've modified the XPath of my element (cellByNum) with a variable for the row:
div[3]/div[2]/div/div[2]/table/tbody/tr[$rowNum]

In Ranorex Spy, when I replace $rowNum with actual values, the expected rows are highlighted. However, in my user code, when I replace my variable there (also called rowNum... I know, I know, I'll fix it) with actual values, absolutely nothing happens so I know that I'm doing something very wrong... sigh.

This is my user code to try to click the row:
repo.BlazorClient.IndividualsPage.cellByNum.Click(rowNum);

If someone could please tell me how I should be doing this, I would greatly appreciate it.

Thanks so much.
You do not have the required permissions to view the files attached to this post.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Clicking a Specified Row in HTML Table

Post by odklizec » Wed Apr 06, 2022 7:45 am

Hi,

This does not make sense:
repo.BlazorClient.IndividualsPage.cellByNum.Click(rowNum);
Click method does not accept "row number" as a parameter. So entering 'rowNum' in Click method is useless.

If the repo variable rowNum is connected to a data connector of your choice (containing the rowNumber) and properly filled, it should be enough to use Click method without parameter...
repo.BlazorClient.IndividualsPage.cellByNum.Click();
Anyway, the whole concept of clicking cell by its row number or index is weird and potentially failing. Why don't you click the cell containing certain name? Or a combination of first and last name?

For example, you can use xpath like this, to find the appropriate cell by the 'Last Name':

Code: Select all

/dom[@domain='localhost:5001']//table[@class='mud-table-root']//td[@data-label='Last Name' and @innertext='Ahle']
Or both Last and First Name:

Code: Select all

/dom[@domain='localhost:5001']//table[@class='mud-table-root']//td[@data-label='Last Name' and @innertext='Ahle']/ancestor::tr/td[@data-label='First Name' and @innertext='Delmy']
Then simply replace the 'innertext' strings with variables and connect them to a data connector of your choice.

Also, I would suggest not to call the repo items directly in code, but rather pass the repo item as a parameter to user code method.

Code: Select all

public void ClickTableCell(RepoItemInfo repoElement)
{
    Ranorex.TdTag cellAdapter = repoElement.CreateAdapter<Ranorex.TdTag>(false);
    cellAdapter.Click();
}
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

mcs
Posts: 35
Joined: Mon Aug 11, 2014 5:00 pm
Location: Portsmouth, NH

Re: Clicking a Specified Row in HTML Table

Post by mcs » Wed Apr 06, 2022 1:18 pm

Thanks, Pavel.

Clearly, I need to refresh my C# coding skills. I do understand what you're saying; my intent was to randomly click a row (but I suppose that I could randomly click a cell instead) and then I'm doing a compare of what appears in the row selected and what appears in the header (that part I have working).

I think that you've, again, pointed me the right direction.

Thanks again.

mcs
Posts: 35
Joined: Mon Aug 11, 2014 5:00 pm
Location: Portsmouth, NH

Re: Clicking a Specified Row in HTML Table

Post by mcs » Thu Apr 07, 2022 5:18 pm

Hello,

I just wanted to post an update on this since I was actually able to implement what I was attempting to do from the start.

What finally worked was the following:
Created a table adapter for the HTML table, then iterated through the HTML table to get the ChildIndex of each row. The ChildIndex corresponds to my loop counter (iRow). I have a separate method getting a random int. When iRow is equal to my random int, the row is clicked.

It took me a while to get here and the assistance from Pavel really helped!

Thanks again. :D