scrollIntoView() 不适用于水平滚动(Selenium) [英] scrollIntoView() not working for horizontal scroll (Selenium)
问题描述
我想向右滚动到一列.这是我试过的:
i want to scroll right to a column. This is what i tried:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
这适用于垂直滚动,但不适用于水平滚动.
This works for vertical scrolling but not for horizontal.
推荐答案
Element.scrollIntoView()
Element.scrollIntoView() 方法滚动元素被调用到浏览器窗口的视口.
Element.scrollIntoView()
Element.scrollIntoView() method scrolls the element on which it's called into the Viewport of the browser window.
element.scrollIntoView()
element.scrollIntoView(alignToTop)
//布尔参数element.scrollIntoView(scrollIntoViewOptions)
//对象参数
element.scrollIntoView()
element.scrollIntoView(alignToTop)
// Boolean parameterelement.scrollIntoView(scrollIntoViewOptions)
// Object parameter
这个方法的参数是:
alignToTop
(可选):是一个布尔值,如果为true,元素的顶部将与可滚动祖先的可见区域的顶部对齐.这是默认值.如果为 false,则元素的底部将与可滚动祖先的可见区域的底部对齐.对应 scrollIntoViewOptions: {block: "end", inline: "nearest"}.scrollIntoViewOptions
(可选):是一个具有以下属性的对象:behavior
(可选):定义过渡动画.自动"或平滑"之一.默认为自动".block
(可选):定义垂直对齐方式.开始"、中心"、结束"或最近"之一.默认为开始".inline
(可选):定义水平对齐方式.开始"、中心"、结束"或最近"之一.默认为最近的".
alignToTop
(Optional): Is a Boolean value, if true, the top of the element will be aligned to the top of the visible area of the scrollable ancestor. This is the default value. If false, the bottom of the element will be aligned to the bottom of the visible area of the scrollable ancestor. Corresponds to scrollIntoViewOptions: {block: "end", inline: "nearest"}.scrollIntoViewOptions
(Optional): Is an Object with the following properties:behavior
(Optional): Defines the transition animation. One of "auto" or "smooth". Defaults to "auto".block
(Optional): Defines vertical alignment. One of "start", "center", "end", or "nearest". Defaults to "start".inline
(Optional): Defines horizontal alignment. One of "start", "center", "end", or "nearest". Defaults to "nearest".
根据您的代码行:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
参数
true
指的是alignToTop
的布尔值.因此问题.the argument
true
refers to boolean value foralignToTop
. Hence the issue.要自动水平滚动,您需要为
inline
参数传递以下任一参数:-开始
-中心
-结束
-最近的
To automate horizontal scrolling you need to pass the argument for the
inline
parameter either among the following: -start
-centre
-end
-nearest
作为替代,您可以使用以下任一选项
As an alternative you can use either of the following options
scrollLeft()
:Element.scrollLeft()
属性获取或设置元素内容从其左边缘滚动的像素数.如果元素的 direction 是rtl
(从右到左),则当滚动条位于最右侧位置(滚动内容的开头)时scrollLeft
为0
,然后在您滚动到内容末尾时越来越消极.scrollWidth()
:Element.scrollWidth()
只读属性是对元素内容宽度的度量,包括由于溢出而在屏幕上不可见的内容.
scrollLeft()
:Element.scrollLeft()
property gets or sets the number of pixels that an element's content is scrolled from its left edge. If the element's direction isrtl
(right-to-left), thenscrollLeft
is0
when the scrollbar is at its rightmost position (at the start of the scrolled content), and then increasingly negative as you scroll towards the end of the content.scrollWidth()
:Element.scrollWidth()
read-only property is a measurement of the width of an element's content, including content not visible on the screen due to overflow.
您可以在以下位置找到一些相关的详细讨论:
You can find a couple of relevant detailed discussion in:
这篇关于scrollIntoView() 不适用于水平滚动(Selenium)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!