Selenium,你如何检查滚动位置

时间:2022-03-11 20:48:17

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?

使用selenium和java,我需要测试一个“返回顶部”按钮,所以我做的是向下滚动页面,直到显示“返回顶部”按钮(当滚动页面的25%时显示)并单击它,此按钮将用户带到页面顶部,现在我需要检查它是否有效,可见部分是页面的顶部。我怎么能用java做到这一点?

2 个解决方案

#1


17  

The general principle is to check the value of window.pageYOffset in the browser. If your button scrolls completely back 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.假设驱动程序变量保存您的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.

然后,您可以检查该值是否为0. executeScript用于在浏览器中运行JavaScript代码。

This answer initially mentioned scrollY but there's no support for it on IE. The MDN page on it, says:

这个答案最初提到了scrollY但在I​​E上没有它的支持。它上面的MDN页面说:

For cross-browser compatibility, use window.pageYOffset instead of window.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:

对于跨浏览器兼容性,请使用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. Oosterholt for the "heads up".

感谢R. Oosterholt的“抬头”。

#2


4  

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'访问驱动程序实例):

driver.execute_script('return window.pageYOffset;')

#1


17  

The general principle is to check the value of window.pageYOffset in the browser. If your button scrolls completely back 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.假设驱动程序变量保存您的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.

然后,您可以检查该值是否为0. executeScript用于在浏览器中运行JavaScript代码。

This answer initially mentioned scrollY but there's no support for it on IE. The MDN page on it, says:

这个答案最初提到了scrollY但在I​​E上没有它的支持。它上面的MDN页面说:

For cross-browser compatibility, use window.pageYOffset instead of window.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:

对于跨浏览器兼容性,请使用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. Oosterholt for the "heads up".

感谢R. Oosterholt的“抬头”。

#2


4  

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'访问驱动程序实例):

driver.execute_script('return window.pageYOffset;')