Find an element on page 3 of 3

I have a Page that displays 50 items per page.
The element I’m looking for is located on page 3 of 3.
When page 1 of 3 appears, the first 50 items are displayed, and I search using an XPath command like:
Set Local Variable ${xpath_with_item} //span[text()=${item_number}]
${result} Get Text ${xpath_with_item}
My call to “Get Text” times out because ${item_number} is located on page 3.
I’m still learning Browser Library Keywords, and I’m sensing I’ll have to loop around, but I would like to learn what Browser Library Keywords are recommended to be used in situations like this?
Thank you for your patience.

I don’t use Browser library, but this question would also be applicable to SeleniumLibrary, because there is no specialized keyword for what you want.

You need to do some calculations to get the element you want. For example:

  • Is the element in the referenced by absolute indexing, like in x > 50 * 2
  • Is the element referenced by relative page position, like in 0 <= x < 50 and page_num == 2

It is a matter of doing simple page and rows calculation.

Hi Paul,

The “Magic” your after is Run Keyword And Return Status which you combine with something like Page Should Contain Element followed by an IF statement to decide if you should loop around or click the item.

If there is a likelihood that in some future version of your application the item you want might be on page 4 of 4 or 6 of 7, then you might want to put this in a recursive keyword (a keyword that calls itself in the if statement), I gave an example of this here

Hope that helps,

Dave.

2 Likes

Thank you both @damies13 and @HelioGuilherme66 for your helpful suggestions! I did use your ideas and learned some things along the way.

I focused on Library Browser and Library Collections to accomplish my task.
This meant I would not be using Keyword “Page Should Contain Element” since “Page Should Contain Element” was from SeleniumLibrary and my Browser would be launched from Library Browser.

I learned WHILE, and BREAK Keywords are “Reserved Keywords” and are not valid inside programmer-defined Keywords. This meant that my approaches to my solution using Robot Frameworks Iteration inside my programmer-defined Keywords had to be aborted. I was left with either creating a solution using a Library of my on creation, or using recursion, as was suggested by @damies13.

I chose to use recursion in my new programmer-defined Keyword, and I was successful!

I’m going to post my experimental code here (NOTE: Fake URL’s, usernames, and passwords are shown here in the code below. I don’t consider that important as much as understanding how to use recursion in a Robot Framework Keyword). Here was my approach - again, thank you guys for sharing your ideas!

Blockquote
*** Settings ***
Library Browser
Library Collections
Suite Setup Initialize Run
Suite Teardown Cleanup and Close

*** Variables ***
${CONTROL_CENTER_URL} https://testing.someserver.com/auth/login
${EMAIL_FIELD} id=Email
${FIRST_PAGE} 1 # List of Locks begins on page 1
${LOGIN_BTN} id=Login
${LOGOUT_BTN} id=logout
${LOGIN_SUBMIT_BTN} xpath=//button[@type=‘submit’]
${PASSWORD} some!password321
${PASSWORD_FIELD} id=Password
${SECS_BETWEEN_CHECKS} 2s
${USER_EMAIL} dude@somecompany.com
${USER_MENU_BTN} id=userMenuBtn

*** Keywords ***
Bring Up Control Center
New Browser chromium headless=false timeout=0:01:30
New Context viewport={‘width’: 1920, ‘height’: 1080}
New Page url=${CONTROL_CENTER_URL}
Get Title *= Control Center

Build XPath With Lock Number
[Arguments] ${lock_number}
Set Local Variable ${xpath_with_number} //span[text()=${lock_number}]
[Return] ${xpath_with_number}

Cleanup and Close
[Documentation] Logs suite status.
Control Center Logout

Control Center Login
Click ${LOGIN_BTN}
Fill Text ${EMAIL_FIELD} ${USER_EMAIL}
Fill Text ${PASSWORD_FIELD} ${PASSWORD}
Click ${LOGIN_SUBMIT_BTN}

Control Center Logout
Click ${USER_MENU_BTN}
Sleep ${SECS_BETWEEN_CHECKS}
Click ${LOGOUT_BTN}
Sleep ${SECS_BETWEEN_CHECKS}

Find Lock
[Arguments] ${lock_number} ${page_idx}
Set Local Variable ${result} Lock not found
${xpath_with_number} Build XPath With Lock Number ${lock_number}
${is_visible} Lock Shown On Page ${xpath_with_number}
${page_idx} Evaluate ${page_idx} + 1
${has_next_page_btn} Has Next Page ${page_idx}
IF ‘${is_visible}’ == ‘${False}’
IF ‘${has_next_page_btn}’ == ‘${True}’ # Advance to next Lock screen
Click xpath=//button[@class=‘mat-focus-indicator mat-icon-button mat-button-base’][${page_idx}]
Sleep ${SECS_BETWEEN_CHECKS}
${result} Find Lock ${lock_number} ${page_idx}
END
ELSE # Obtain the Lock number
${result} Get Text ${xpath_with_number}
END
Should Be Equal As Strings ${result} ${lock_number} Find Lock Error. Encountered ${result} expected ${lock_number}
[Return] ${result}

Has Next Page
[Arguments] ${page_idx}
${elements_list} Get Elements xpath=//button[@class=‘mat-focus-indicator mat-icon-button mat-button-base’][${page_idx}]
${has_next_page_btn} Evaluate len(${elements_list}) > 0
[Return] ${has_next_page_btn}

Lock Shown On Page
[Arguments] ${xpath_with_number}
@{element_list} Get Elements xpath=${xpath_with_number}
${is_visible} Evaluate len(${element_list}) > 0
[Return] ${is_visible}

Initialize Run
Bring Up Control Center
Control Center Login

*** Test Cases ***
SmartLock InList
Find Lock ${SMARTLOCK_LOCKED_ID} ${FIRST_PAGE}
Blockquote

1 Like