web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Automate
Answered

Table Error

(0) ShareShare
ReportReport
Posted on by 32

Hello, I have a JavaScript that I would like to run to account the number of rows that appears after filtering. The filtering script is working, but when I run the script to count the row count it returns [object Object], but when I run the script directly in the browser, it correctly returns 4. Does anyone have any insight for what I could be doing wrong? Here's the script: 

 

function ExecuteScript() {var rowCount;

// Define a function to count rows
function countRows(container) {
  // Initialize a counter
  rowCount = 0;

  // Check if the container exists
  if (container) {
    // Find all elements with role="row" within the container
    var rows = container.querySelectorAll('[role="row"]');

    // Count the number of elements found
    rowCount = rows.length;
  }

  // Return the count
  return rowCount;
}

// Call the countRows function with the container variable as an argument to update the global variable rowCount
countRows(document.querySelector('.x-grid-item-container')); 

return rowCount;}
ExecuteScript();

I have the same question (0)
  • Deenuji_Loganathan_ Profile Picture
    6,255 Moderator on at

    @123letii 

     

    I have slightly modified your code[Removed last line on your code]. Please check and implement similar code for your use case.

    Deenuji_0-1712907413117.png

     

     

    Flow source code:

     

     

    WebAutomation.LaunchChrome.LaunchChrome Url: $'''https://afd.calpoly.edu/web/sample-tables''' WindowState: WebAutomation.BrowserWindowState.Maximized ClearCache: False ClearCookies: False WaitForPageToLoadTimeout: 60 Timeout: 60 PiPUserDataFolderMode: WebAutomation.PiPUserDataFolderModeEnum.AutomaticProfile BrowserInstance=> Browser
    WebAutomation.ExecuteJavascript BrowserInstance: Browser Javascript: $'''function ExecuteScript() {var rowCount;
    
    // Define a function to count rows
    function countRows(container) {
     // Initialize a counter
     rowCount = 0;
    
     // Check if the container exists
     if (container) {
     // Find all elements with role=\"row\" within the container
     var rows = container.querySelectorAll(\'tbody tr\');
    
     // Count the number of elements found
     rowCount = rows.length;
     }
    
     // Return the count
     return rowCount;
    }
    
    // Call the countRows function with the container variable as an argument to update the global variable rowCount
    countRows(document.querySelector(\'.table_directory tbody\')); 
    
    return rowCount;}
    
    
    
    
    
    
    
    
    
    
    
    ''' Result=> Result

     

     

     

     

    How to copy/paste above code into your PAD?

    Deenuji_1-1712907090918.gif

     


    Thanks,
    Deenuji Loganathan 👩‍💻
    Automation Evangelist 🤖
    Follow me on LinkedIn 👥

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀

     
     
  • eetuRobo Profile Picture
    4,574 Super User 2026 Season 1 on at

    Try without ExecuteScript();

    So just remove the last line from your code.

    eetuRobo_0-1712907091202.png

     

    [object Object] usually indicates an error

    You could try also adding .toString(); to return rowCount;
      return rowCount.toString();
    So maybe it converts it to something that makes sense (usually not).

    Also make sure your Run JavaScript function on web page is using the correct browser instance.


  • 123letii Profile Picture
    32 on at

    hello! Thank you for your reply. Unfortunately, that didn't work. Although now I am not getting [object Object], I am just getting 0. Do you have any other insight for why this might be happening? Thank you!

  • 123letii Profile Picture
    32 on at

    hello! Thank you for your reply. Unfortunately, that didn't work. Although now I am not getting [object Object], I am just getting 0. Do you have any other insight for why this might be happening? Thank you!

  • eetuRobo Profile Picture
    4,574 Super User 2026 Season 1 on at

    Then I think the JavaScript is working but just selecting wrong element. Are you sure its selecting correct element so the table and its rows?
    In my example its getting id that is named "example"(so my example table has that id)
    under that tbody and under that that tr

    eetuRobo_1-1713077087870.png


    I'm just using this site to create the example: https://datatables.net/

  • Deenuji_Loganathan_ Profile Picture
    6,255 Moderator on at

    @123letii 

     

    Could you kindly provide screenshots of the HTML elements by pressing F12 and selecting the appropriate element that you want to target in order to retrieve the number of rows?

     


    Thanks,
    Deenuji Loganathan 👩‍💻
    Automation Evangelist 🤖
    Follow me on LinkedIn 👥

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀

     
  • 123letii Profile Picture
    32 on at

    Yes! I have also tried this script, and I am still returning 0 in power automate. But when I run it directly in the browser, and call the function, I get 4

    function ExecuteScript() {
        function countUniqueTableIds() {
            // Select the div with class "x-grid-item-container"
            var div = document.querySelector('.x-grid-item-container');
            
            // If the div exists
            if (div) {
                // Get all tables within the div
                var tables = div.getElementsByTagName('table');
                
                // Initialize an object to store unique table IDs
                var uniqueIds = {};
                
                // Loop through each table
                for (var i = 0; i < tables.length; i++) {
                    // Get the ID of the current table
                    var tableId = tables[i].id;
                    
                    // If the table has an ID
                    if (tableId) {
                        // Store the ID in the uniqueIds object
                        uniqueIds[tableId] = true;
                    }
                }
                
                // Count the number of unique table IDs
                var count = Object.keys(uniqueIds).length;
                
                // Return the count
                return count;
            } else {
                // If the div does not exist, return -1
                return -1;
            } 
        }
        
        // Call countUniqueTableIds() and return its result
        return countUniqueTableIds();
    }

     

    123letii_0-1714705122946.png

     

    I have also uploaded the HTML. Thank you so much! 

  • Verified answer
    123letii Profile Picture
    32 on at

    Oh I got it to work! 

    This was the script: 

    function ExecuteScript() {
        function countUniqueTableIds() {
            // Select the div with class "x-grid-item-container"
            var div = document.querySelector('.x-grid-item-container');
            
            // If the div exists
            if (div) {
                // Get all tables within the div
                var tables = div.getElementsByTagName('table');
                
                // Initialize an object to store unique table IDs
                var uniqueIds = {};
                
                // Loop through each table
                for (var i = 0; i < tables.length; i++) {
                    // Get the ID of the current table
                    var tableId = tables[i].id;
                    
                    // If the table has an ID
                    if (tableId) {
                        // Store the ID in the uniqueIds object
                        uniqueIds[tableId] = true;
                    }
                }
                
                // Count the number of unique table IDs
                var count = Object.keys(uniqueIds).length;
                
                // Return the count
                return count;
            } else {
                // If the div does not exist, return -1
                return -1;
            } 
        }
        
        // Call countUniqueTableIds() and return its result
        return countUniqueTableIds();
    }

    Thank you both for your attention and help! 

  • 123letii Profile Picture
    32 on at

    Hello! Thank you for your replies. 

     

    I still have not managed to overcome this. I tried using a new script (below), which when I run in the browser returns 4, but when I put it in power automate, it returns 0. Also attaching the HTML. 

     

    function ExecuteScript() {
        function countUniqueTableIds() {
            // Select the div with class "x-grid-item-container"
            var div = document.querySelector('.x-grid-item-container');
            
            // If the div exists
            if (div) {
                // Get all tables within the div
                var tables = div.getElementsByTagName('table');
                
                // Initialize an object to store unique table IDs
                var uniqueIds = {};
                
                // Loop through each table
                for (var i = 0; i < tables.length; i++) {
                    // Get the ID of the current table
                    var tableId = tables[i].id;
                    
                    // If the table has an ID
                    if (tableId) {
                        // Store the ID in the uniqueIds object
                        uniqueIds[tableId] = true;
                    }
                }
                
                // Count the number of unique table IDs
                var count = Object.keys(uniqueIds).length;
                
                // Return the count
                return count;
            } else {
                // If the div does not exist, return -1
                return -1;
            } 
        }
        
        // Call countUniqueTableIds() and return its result
        return countUniqueTableIds();
    }

     

     

    123letii_0-1714706915787.png

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 238 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 225

#3
Haque Profile Picture

Haque 181

Last 30 days Overall leaderboard