In Selenium WebDriver, there are two major methods to put an element into a visible area:
Scrolling into view:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Using moveToElement
browser action:
Actions actions = new Actions(driver); actions.moveToElement(element); actions.perform();
Are these methods equivalent and which one should be preferred?
Selenium can execute commands in Javascript with the help of the execute_script() method. For the Javascript solution, we have to pass true value to the method scrollIntoView() to identify the object below our current location on the page. We can execute mouse movement with the help of the Actions class in Selenium.
scrollIntoView(true);", element); This works for vertical scrolling but not for horizontal.
The executeScript MethodscrollIntoView(true);", element); This code executes JavaScript scrollIntoView, which causes our browser to scroll to the view specified. The second parameter of executeScript() is the element we wish to scroll to, and arguments[0] refers to the first element in a list of parameters.
Selenium cannot handle scrolling directly. It takes the help of the Javascript Executor to do scrolling action to a specific DIV. First of all we have to identify the specific DIV up to which we have to scroll to with the help of xpath or css locator.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With