Selenium, how do you check scroll position

后端 未结 3 689
我寻月下人不归
我寻月下人不归 2021-02-07 11:00

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% o

3条回答
  •  没有蜡笔的小新
    2021-02-07 11:24

    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.

    Source: 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'):

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

提交回复
热议问题