scrollIntoView vs moveToElement

50,883

scrollIntoView

The DOM method scrollIntoView only scrolls the element into view. If scrollIntoView cannot scroll the element into view, it will just fail silently.I added an invisible element to the start of body and called scrollIntoView on it. Nothing scrolled but there was no error. Note that you have more control on how the element is scrolled with scrollIntoView than with moveToElement. Selenium is only interested in bringing the element into view so that the mouse can be placed on it. It does not give you any say in how it is going to do it. scrollIntoView however allows you, for instance, to specify whether you want the top or bottom of the element to be align with its scrollable ancestor. (See here for the details.)

moveToElement

The Selenium method moveToElement does two things: it scrolls the element into view and moves the mouse on top of the element. I've also tested it with elements that cannot be scrolled or moved to because they have no coordinates on screen and got no error here either.

Choosing One

I default to using moveToElement, with the following exceptions:

  • If you do not want to affect at all where Selenium has placed the mouse but you want to scroll something into view (a bit strange... but possible), then you should use scrollIntoView.

  • If you need to scroll an element with the kind of control that scrollIntoView gives you (like the alignment option I mentioned above), then you have to use it rather than moveToElement.

  • There are cases where trying to simulate user behavior through Selenium's commands is not possible or is very expensive to do by sending a series of Selenium commands. (Each command is a round-trip to the network. When the testing server somewhere across the Internet, it adds up.) In such cases, I use Selenium's executeScript. In such case, it can be advantageous to use scrollIntoView in the script being executed, rather then end the script, create an Action to perform the scroll, and finish the whole operation with another executeScript.

Share:
50,883

Related videos on Youtube

alecxe
Author by

alecxe

"I am a soldier, at war with entropy itself" I am a Software Developer and generalist who is in love with the Python language and community. I greatly value clean and maintainable code, great software, but I know when I need to be a perfectionist and when it stands in a way of product delivery. I like to break things, to find new ways to break things, to solve hard problems, to put things under test and stress, and to have my mind blown by an interesting question. Some of my interests: Learning, Productivity, AI, Space Exploration, Internet of Things. "If you change the way you look at things, the things you look at change." - Wayne Dyer If you are looking for a different way to say "Thank you": Amazon wish list Pragmatic wish list Today I left my phone at home And went down to the sea. The sand was soft, the ocean glass, But I was still just me. Then pelicans in threes and fours, Glided by like dinosaurs, An otter basked upon its back, And dived to find another snack. The sun corpuscular and bright, Cast down a piercing shaft, And conjured an inspiring sight On glinting, bobbing craft. Two mermaids rose up from the reef, Out of the breaking waves. Their siren song was opium grief, Their faces from the grave. The mermaids asked a princely kiss To free them from their spell. I said to try a poet’s bliss. They shrugged and bid farewell. The sun grew dark and sinister, In unscheduled eclipse. As two eight-headed aliens Descended in their ships. They said the World was nice enough But didn’t like our star. And asked the way to Betelgeuse, If it wouldn’t be too far. Two whales breached far out to sea, And flew up to the sky, The crowd was busy frolicking, And didn’t ask me why. Today I left my phone at home, On the worst day, you’ll agree. If only I had pictures, If only you could see. Not everything was really there, I’m happy to confess, But I still have the memories, Worth more than tweets and stress. Today I left my phone at home, I had no shakes or sorrow. If that is what my mind can do, It stays at home tomorrow. Gavin Miller

Updated on April 12, 2020

Comments

  • alecxe
    alecxe about 4 years

    In Selenium WebDriver, there are two major methods to put an element into a visible area:

    1. Scrolling into view:

      ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
      
    2. Using moveToElement browser action:

      Actions actions = new Actions(driver);
      actions.moveToElement(element);
      actions.perform();
      

    Are these methods equivalent and which one should be preferred?

  • Alexandre Assouad
    Alexandre Assouad about 4 years
    moveToElement don't scroll to the element if it is not visible on screen. It only controls the mouse and put the cursor on the element. If you want to scroll to an invisible element, you'll have to tell selenium to scroll the page until the element position is visible on the screen (you can compare element location with viewport)
  • MasterJoe
    MasterJoe about 4 years
    So far have see that JS scrollIntoView always works, but Actions moveTo fails. Why does JS seem to be more reliable ?