如何用Robot Framework统计多页表格的总行数?
Fixing Pagination Row Count Issue in Robot Framework
Hey there! I totally get your frustration—dealing with pagination when trying to count total table rows is such a common gotcha, especially when you're still getting up to speed with Robot Framework. Let's walk through a couple of practical solutions based on how your Chrome app handles pagination:
Option 1: Grab the Total Count Directly (If Available)
Many apps display the total number of results somewhere on the page (like "Showing 1-5 of 76 results"). If your app does this, this is the fastest and most reliable approach:
- Locate the element that shows the total count text
- Extract the numeric value from that text using regex (way more reliable than hardcoding index positions)
Example code:
# Get the total count text from the page (update the locator to match your app) ${total_results_text}= Get Text //div[@class='results-summary'] # Extract the numeric total using regex ${total_rows_match}= Get Regexp Matches ${total_results_text} (\d+) ${total_rows}= Convert To Integer ${total_rows_match}[0] Log Total table rows: ${total_rows}
Option 2: Traverse All Pages and Count Rows
If your app doesn't show the total count upfront, you'll need to loop through each page, count the rows on each, and add them up. Here's how to do it safely:
Step-by-Step Code:
# Initialize total count to 0 ${total_count}= Set Variable 0 # Loop through pages (set a reasonable upper limit to avoid infinite loops) :FOR ${page_num} IN RANGE 1 100 \ # Count rows on the current page \ ${current_rows}= Get Element Count //table[@class='options-table']/tbody/tr \ # Add current page rows to total \ ${total_count}= Evaluate ${total_count} + ${current_rows} \ # Check if the next page button is still clickable \ ${next_page_available}= Run Keyword And Return Status Element Should Be Enabled //button[@class='next-page-btn'] # Update this locator \ # Exit loop if there's no next page \ Exit For Loop If not ${next_page_available} \ # Click to next page \ Click Element //button[@class='next-page-btn'] \ # Wait for the next page to load (critical to avoid counting old rows) \ Wait Until Page Contains Element //table[@class='options-table']/tbody/tr timeout=10 # Log the final total Log Total rows across all pages: ${total_count}
Key Tips for This Approach:
- Always wait for page load: Adding a wait after clicking next page ensures the new rows are fully loaded before you count them.
- Adjust the next page locator: Some apps use
<a>tags instead of buttons, or disable the next button with adisabledattribute, or hide it entirely on the last page—tweak the locator to match your app's behavior. - Prevent infinite loops: Setting an upper limit (like 100) in the
:FORloop stops the script from running forever if something goes wrong with the next page check.
内容的提问来源于stack exchange,提问作者Laura Kendall
相关产品推荐
相关产品推荐

