scrollIntoView vs moveToElement [英] scrollIntoView vs moveToElement
问题描述
-
滚动查看:
$ b $((JavascriptExecutor)驱动程序).executeScript(arguments [0] .scrollIntoView(true);,element);
-
操作actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();
这些方法是否相同,哪一个应该是首选?
scrollIntoView
DOM方法 scrollIntoView
仅将元素滚动到视图中。如果 scrollIntoView
无法将元素滚动到视图中,它将会静默失败。我在 body 并在其上调用
scrollIntoView
。没有滚动,但没有错误。请注意,您对 元素使用 scrollIntoView
滚动的方式比 moveToElement
。 Selenium只想将元素放入视图中,以便鼠标可以放在上面。它不会告诉你如何去做。然而,例如, scrollIntoView
允许您指定元素的顶部还是底部与其可滚动祖先对齐。 (有关详细信息,请参见此处。)
moveToElement
Selenium方法 moveToElement
做两件事:它将元素滚动到视图中,并将鼠标移动到元素的顶部。我还用无法滚动或移动的元素对它进行测试,因为它们在屏幕上没有坐标,在这里也没有错误。
选择一个
我默认使用 moveToElement
,但以下情况除外:
-
如果您不想影响Selenium放置鼠标的所有位置,但您希望将东西滚动到视图中(有点奇怪......但可能),那么你应该使用
scrollIntoView
。如果您需要使用scrollIntoView
类型的控件滚动某个元素(例如对齐选项我上面提到),那么你必须使用它而不是moveToElement
。 -
通过Selenium命令尝试模拟用户行为的情况是不可能的,或者通过发送一系列Selenium命令来实现这种情况非常昂贵。 (每个命令都是往返于网络的,当测试服务器跨越互联网时,它就会相加)。在这种情况下,我使用Selenium的
executeScript
。在这种情况下,在正在执行的脚本中使用scrollIntoView
会比较有利,而不是结束脚本,创建一个Action
执行滚动,并用另一个executeScript
完成整个操作。
In Selenium WebDriver, there are two major methods to put an element into a visible area:
((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?
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 thanmoveToElement
.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 usescrollIntoView
in the script being executed, rather then end the script, create anAction
to perform the scroll, and finish the whole operation with anotherexecuteScript
.
这篇关于scrollIntoView vs moveToElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!