[英]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() method scrolls the element on which it's called into the Viewport of the browser window. Element.scrollIntoView()方法将调用它的元素滚动到浏览器 window 的视口中。
element.scrollIntoView()
element.scrollIntoView(alignToTop)
// Boolean parameter element.scrollIntoView(alignToTop)
// Boolean 参数element.scrollIntoView(scrollIntoViewOptions)
// Object parameter element.scrollIntoView(scrollIntoViewOptions)
// Object 参数The parameters for this method are:该方法的参数为:
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. alignToTop
(可选):是一个 Boolean 值,如果为 true,则元素的顶部将与可滚动祖先的可见区域的顶部对齐。 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.如果为 false,则元素的底部将与可滚动祖先的可见区域的底部对齐。 Corresponds to scrollIntoViewOptions: {block: "end", inline: "nearest"}.对应于 scrollIntoViewOptions: {block: "end", inline: "nearest"}。scrollIntoViewOptions
(Optional): Is an Object with the following properties: scrollIntoViewOptions
(可选):是具有以下属性的 Object:
behavior
(Optional): Defines the transition animation. behavior
(可选):定义转换 animation。 One of "auto" or "smooth". “自动”或“平滑”之一。 Defaults to "auto".默认为“自动”。block
(Optional): Defines vertical alignment. block
(可选):定义垂直 alignment。 One of "start", "center", "end", or "nearest". “开始”、“中心”、“结束”或“最近”之一。 Defaults to "start".默认为“开始”。inline
(Optional): Defines horizontal alignment. inline
(可选):定义水平 alignment。 One of "start", "center", "end", or "nearest". “开始”、“中心”、“结束”或“最近”之一。 Defaults to "nearest".默认为“最近”。As per your line of code:根据您的代码行:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
the argument true
refers to boolean value for alignToTop
.参数true
指的是 alignToTop 的alignToTop
值。 Hence the issue.因此问题。
To automate horizontal scrolling you need to pass the argument for the inline
parameter either among the following: - start
- centre
- end
- nearest
要自动水平滚动,您需要传递以下inline
参数的参数: - start
- centre
- end
- nearest
As an alternative you can use either of the following options作为替代方案,您可以使用以下任一选项
scrollLeft()
: Element.scrollLeft()
property gets or sets the number of pixels that an element's content is scrolled from its left edge. scrollLeft()
: Element.scrollLeft()
属性获取或设置元素内容从其左边缘滚动的像素数。 If the element's direction is rtl
(right-to-left), then scrollLeft
is 0
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.如果元素的方向是rtl
(从右到左),那么当滚动条位于其最右侧 position(在滚动内容的开头)时, scrollLeft
为0
,然后随着您向内容的末尾滚动而逐渐变负。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. scrollWidth()
: Element.scrollWidth()
只读属性是元素内容宽度的度量,包括由于溢出而在屏幕上不可见的内容。You can find a couple of relevant detailed discussion in:您可以在以下位置找到一些相关的详细讨论:
you can use below option to scroll Horizontal您可以使用以下选项滚动水平
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementById('giveLocationHorizontal').scrollLeft += 250", "");
Apart from that I assume below are also useful for you除此之外,我认为以下内容对您也有用
//scroll up web page
public void scrollUp(){
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,-250)", "");
}
//scroll down web page
public void scrollDown(){
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,250)", "");
}
//scroll Horizontal
public void scrollHorizontal(){
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(250,0)", "");
}
You can try with below if you have id如果你有身份证,你可以试试下面
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript(
"document.getElementById('gvLocationHorizontalRail').scrollLeft += 250", "")
OR或者
jse.executeScript("window.scrollBy(1000,0)", "");
Source:资源:
http://www.softwaretestingstudio.com/scroll-selenium-webdriver-java/ http://www.softwaretestingstudio.com/scroll-selenium-webdriver-java/
OR By Using actions或通过使用操作
WebElement horizontalbar = driver.findElement(By.id("board") );
Actions action = new Actions(driver);
Actions moveToElement = action.moveToElement( horizontalbar );
for (int i = 0; i < 5; i++) {
moveToElement.sendKeys(Keys.RIGHT).build().perform();
}
Source:资源:
Selenium: horizontal scroll using Actions class Selenium:水平滚动使用动作 class
If you have the th tag of the column this should allow you to scroll to it如果您有该列的 th 标签,这应该允许您滚动到它
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.querySelector('table th:your-child').scrollIntoView();");
You can try one of below send keys:您可以尝试以下发送密钥之一:
element.sendKeys("\n");
element.sendKeys(Keys.SHIFT);
element.sendKeys(Keys.TAB);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.