Search code examples
javascriptrobotframework

Scrolling on a page to a locator using Robot Framework


I have been working with robot framework on a page written in java/java script. I am trying to scroll to a locator to be optically visible on the page to be able to verify its text value. I did follow this QA based on my question and found a solution, however it pops me an exception.

Unable to scroll down the web page using the Robot Framework

My locator to scroll to on the page:

${CLAIMS}    xpath=//*[@id="generalAndIncidents:relatedAlertsPanel:idPreviousClaimsList"]

My keyword to do the scrolling (it has one argument for a locator variable to be loaded):

Scroll To Element
[Arguments]    ${scroll_to_element}
Sleep    1s
Execute JavaScript    window.document.evaluate("${scroll_to_element}", document.body, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.scrollIntoView(true);
Sleep    1s

And when running, I receive the following exception:

20:36:17.376    INFO    Executing JavaScript:
window.document.evaluate("xpath=//*[@id="generalAndIncidents:relatedAlertsPanel:idPreviousClaimsList"]", document.body, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.scrollIntoView(true);
Without any arguments.  
20:36:17.758    FAIL    JavascriptException: Message: javascript error: missing ) after argument list
  (Session info: MicrosoftEdge=86.0.622.69)

I also have checked this page for the "how-to" on this java script code snippet:

https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate

Could you kindly suggest me where to start debugging? What am I doing wrong here? Is this happening because I am missing any escapes?

Thank you very much!

Update#1: Could this be something to do with the namespace resolver?

Update#2:

Scroll To Element
    [Arguments]    ${scroll_to_element}
    ${x} =    Get Horizontal Position    ${scroll_to_element}
    ${y} =    Get Vertical Position      ${scroll_to_element}
    Execute Javascript    window.scrollTo(${x}, ${y})

Scroll Loop Click
    [Arguments]    ${locator}
    FOR    ${index}    IN RANGE    1    10
        Sleep    0.5s
        ${isElementVisible} =    Run Keyword and Return Status    Click Element    ${locator}
        Run Keyword If    '${isElementVisible}'!='True'    Wait Until Keyword Succeeds    6s    2s    Scroll To Element    ${locator}
        Log    ${isElementVisible}
        Exit For Loop If    '${isElementVisible}'=='True'
    END
    Wait Until Element is Visible    ${locator}

Solution

  • Can you try the below code,which is currently working for me. Way back got this solution from Stackoverflow.

    Scroll To Element
        [Arguments]  ${locator}
        ${x}=        Get Horizontal Position  ${locator}
        ${y}=        Get Vertical Position    ${locator}
        Execute Javascript  window.scrollTo(${x}, ${y})