How to scroll to an element out of the current screen?

Hello,

I’m working con SeleniumLibrary in a web where a need to go to a locator that is out of the current view. In the “real life” I use the mouse wheel to go down in the screen.

I’m trying to use the “Scroll element into view” but I get an error because the element is out of the bounds of the viewport.

Does anyone know how I “simulate” the scroll down of the mouse wheel or do something similar to move the screen to the desired locator?

Thank you in advance.

1 Like

For some elements that are still off screen after scrolling to (usually a floating toolbar element for us) I have seen products using something like:

Execute Javascript    window.scrollTo(0, window.scrollY+height)

Height being the positive or negative pixel value you want to move by and ScrollY is the current Y position

4 Likes

I’ll test it.

Thank you Brandon

Hi Brandon,

Your suggestion works fine. My problem is using that I scroll all the page but I need to scroll an small area inside the page. How can I scroll only that sub-window?

Thanks in advance.

@vgonzal You will probably have to do something like the responses here: python - How to scroll a particular element(see attached image) horizontally in robot framework? - Stack Overflow

Same problem. Solved fine. Tks!!

Hello @vgonzal

I have already run over a similar problem recently. I noticed a potencial problem with the SeleniunLibrary when you need to reach a element that requires mouse scroll up or down using the keywords Click Button, Click Element, Scroll Element Into View.

I noticed that:

  • The mentioned keywords can find the element
  • The screen is scrolled to the element automatically
  • We can see the element highlighted (in some cases)
  • But the SeleniumLibrary keyword fail

Workaround directly over Robot Framework
I created my own version of Click Button, Click Element, Scroll Element Into View with retries. When the element is out of the screen, you will noticed that in the first execution the original keyword will fail, but it will work in the second attempt.

Click Button Ex
    [Arguments]    ${xpath}  ${attempts}=4
    ${status}    Set Variable    ${False}
    WHILE    ${status} == ${False}
        ${status}    Run Keyword and Return Status    Click Button    ${xpath}
        ${attempts}    Evaluate    ${attempts} - 1
        Run Keyword If  ${attempts} == 0  Fail    ${xpath} not found
    END

Also, regarding to that other problem:

I also run over a similar problem in my current project until I noticed that the “sub-windows” was inside a “iframe”. So I use the following keywords to change the context page (after spend a lot of time on that :smiley: ) :

Select Frame    //iframe[@id="mdhub-module-plataforma.prescricao"]

and

Unselect Frame