Java Selenium,你如何检查滚动位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25733877/
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
Selenium, how do you check scroll position
提问by Yayo
Using selenium with java, I need to test a "Back to top" button, so what I did is to scroll page down until "Back to top" button is shown (as it is shown when scrolled 25% of the page) and click it, this button takes user to the top of the page, now I need to check that it worked and the visible part is the top of the page. How can I do this using java?
在 java 中使用 selenium,我需要测试一个“返回顶部”按钮,所以我所做的是向下滚动页面直到显示“返回顶部”按钮(如滚动页面的 25% 时所示)并单击它,这个按钮将用户带到页面的顶部,现在我需要检查它是否有效并且可见部分是页面的顶部。我如何使用 java 做到这一点?
回答by Louis
The general principle is to check the value of window.pageYOffset
in the browser. If your button scrolls completelyback to the top then window.pageYOffset
should have a value of 0. Assuming the driver
variable holds your WebDriver
instance:
一般的原则是window.pageYOffset
在浏览器中检查 的值。如果您的按钮完全滚动回顶部,则window.pageYOffset
其值应为 0。假设该driver
变量包含您的WebDriver
实例:
JavascriptExecutor executor = (JavascriptExecutor) driver;
Long value = (Long) executor.executeScript("return window.pageYOffset;");
You can then check that value
is 0. executeScript
is used to run JavaScript code in the browser.
然后您可以检查value
0.executeScript
用于在浏览器中运行 JavaScript 代码。
This answer initially mentioned scrollY
but there's no support for it on IE. The MDN page on it, says:
这个答案最初提到过,scrollY
但在 IE 上不支持它。上面的MDN 页面说:
For cross-browser compatibility, use
window.pageYOffset
instead ofwindow.scrollY
. Additionally, older versions of Internet Explorer (< 9) do not support either property and must be worked around by checking other non-standard properties. A fully compatible example:var supportPageOffset = window.pageXOffset !== undefined; var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat"); var x = supportPageOffset ? window.pageXOffset : isCSS1Compat ? document.documentElement.scrollLeft : document.body.scrollLeft; var y = supportPageOffset ? window.pageYOffset : isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop;
为了跨浏览器兼容性,请使用
window.pageYOffset
代替window.scrollY
。此外,旧版本的 Internet Explorer (< 9) 不支持任一属性,必须通过检查其他非标准属性来解决。一个完全兼容的例子:var supportPageOffset = window.pageXOffset !== undefined; var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat"); var x = supportPageOffset ? window.pageXOffset : isCSS1Compat ? document.documentElement.scrollLeft : document.body.scrollLeft; var y = supportPageOffset ? window.pageYOffset : isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop;
Thanks to R. Oosterholtfor the "heads up".
感谢R. Oosterholt的“提醒”。
回答by John Theodore
Louis' answer works, but is not fully cross-browser compatible, as Internet Explorer does not support window.scrollY. I recommend using window.pageYOffset instead - this returns the same value but is cross-browser compatible.
路易斯的回答有效,但不完全跨浏览器兼容,因为 Internet Explorer 不支持 window.scrollY。我建议改用 window.pageYOffset - 这会返回相同的值,但跨浏览器兼容。
Source: https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollY
来源:https: //developer.mozilla.org/en-US/docs/Web/API/Window/scrollY
Here is the above code block with the modified code:
这是上面带有修改代码的代码块:
JavascriptExecutor executor = (JavascriptExecutor) driver;
Long value = (Long) executor.executeScript("return window.pageYOffset;");
Also, syntax for Ruby (what I use for my current position, assuming as before that the driver instance is accessible through the variable name, 'driver'):
此外,Ruby 的语法(我在当前职位上使用的语法,假设驱动程序实例可以通过变量名“驱动程序”访问):
driver.execute_script('return window.pageYOffset;')
回答by Razor
if necessary, find the position scrolled to one specify div, use this code:
如有必要,找到滚动到一个指定 div 的位置,使用以下代码:
JavascriptExecutor executor = (JavascriptExecutor) driver;
Long value = (Long) executor.executeScript("return document.getElementById('container').scrollTop;");