Java scrollIntoView 与 moveToElement
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34562095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
scrollIntoView vs moveToElement
提问by alecxe
In Selenium WebDriver, there are two major methods to put an element into a visible area:
在 Selenium WebDriver 中,有两种主要方法可以将元素放入可见区域:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Using
moveToElement
browser action:Actions actions = new Actions(driver); actions.moveToElement(element); actions.perform();
滚动查看:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Actions actions = new Actions(driver); actions.moveToElement(element); actions.perform();
Are these methods equivalent and which one should be preferred?
这些方法是否等效?应该首选哪一种?
采纳答案by Louis
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 howthe 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 herefor the details.)
DOM 方法scrollIntoView
仅将元素滚动到视图中。如果scrollIntoView
无法将元素滚动到视图中,它将静默失败。我在开头添加了一个不可见元素body
并调用scrollIntoView
了它。没有滚动,但没有错误。请注意,您必须对更多的控制如何将元素与滚动scrollIntoView
比moveToElement
。Selenium 只对让元素进入视图感兴趣,以便鼠标可以放在它上面。它没有给你任何关于它将如何做的发言权。scrollIntoView
但是,例如,允许您指定是否希望元素的顶部或底部与其可滚动祖先对齐。(详情请看这里。)
moveToElement
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.
Selenium 方法moveToElement
做两件事:将元素滚动到视图中并将鼠标移动到元素的顶部。我还使用无法滚动或移动到的元素对其进行了测试,因为它们在屏幕上没有坐标,而且这里也没有错误。
Choosing One
选择一个
I default to using moveToElement
, with the following exceptions:
我默认使用moveToElement
,但有以下例外:
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
.
如果您根本不想影响 Selenium 放置鼠标的位置,但又想将某些内容滚动到视图中(有点奇怪……但可能),那么您应该使用
scrollIntoView
.如果您需要使用
scrollIntoView
为您提供的控件类型滚动元素(如我上面提到的对齐选项),那么您必须使用它而不是moveToElement
.在某些情况下,尝试通过 Selenium 的命令模拟用户行为是不可能的,或者通过发送一系列 Selenium 命令来完成的代价非常高。(每个命令都是到网络的往返。当测试服务器位于 Internet 某处时,它会加起来。)在这种情况下,我使用 Selenium 的
executeScript
. 在这种情况下,最好scrollIntoView
在正在执行的脚本中使用,而不是结束脚本,创建一个Action
来执行滚动,并用另一个完成整个操作executeScript
。