scrollIntoView 与 moveToElement [英] scrollIntoView vs moveToElement
问题描述
在 Selenium WebDriver 中,有两种主要的方法可以将元素放入可见区域:
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
DOM 方法 scrollIntoView
仅将元素滚动到视图中.如果 scrollIntoView
无法将元素滚动到视图中,它只会静默失败.我在 body
的开头添加了一个不可见的元素,并在它.没有滚动,但没有错误.请注意,使用 scrollIntoView
比使用 moveToElement
可以更好地控制如何滚动元素.Selenium 只对显示元素感兴趣,以便可以将鼠标放在它上面.它没有给你任何关于它将如何做的发言权.例如,scrollIntoView
允许您指定是否希望元素的顶部或底部与其可滚动祖先对齐.(有关详细信息,请参阅此处.)
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.)
Selenium 方法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.
我默认使用 moveToElement
,但有以下例外:
I default to using moveToElement
, with the following exceptions:
如果您根本不想影响 Selenium 放置鼠标的位置,但又想将某些内容滚动到视图中(有点奇怪……但可能),那么您应该使用
scrollIntoView代码>.
如果您需要使用 scrollIntoView
为您提供的那种控件滚动元素(如我上面提到的对齐选项),那么您必须使用它而不是 moveToElement
.
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
.
在某些情况下,尝试通过 Selenium 的命令来模拟用户行为是不可能的,或者通过发送一系列 Selenium 命令来模拟用户行为的成本非常高.(每个命令都是到网络的往返.当测试服务器在 Internet 上的某个地方时,它会加起来.)在这种情况下,我使用 Selenium 的 executeScript
.在这种情况下,在正在执行的脚本中使用 scrollIntoView
是有利的,而不是结束脚本,创建一个 Action
来执行滚动,并完成整个操作使用另一个 executeScript
.
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
.
这篇关于scrollIntoView 与 moveToElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!