用户向上滚动时如何停止自动向下滚动?

How to stop the automatic scroll-down when user scroll up?

我想在用户用鼠标向上滚动时停止自动向下滚动。我做了一个功能,每 2 秒自动向下滚动网页,但我想在用户用鼠标向上滚动时停止自动滚动 我的代码:

from selenium import webdriver
import time
driver=webdriver.Chrome() 
driver.get("https://YouTube.com") 
driver. maximize_window() 
a=input('press y if you want to scroll down') 
if a=='y':
    while(True) :                  
          driver. execute_script("window.scrollBy(0, 150) ", "")
          time.sleep(2) 
else:
Print(" Ok") 

     

解决此问题的方法之一是使用 window.pageYOffset 属性。

来自文档

The read-only Window property pageYOffset is an alias for scrollY; as such, it returns the number of pixels the document is currently scrolled along the vertical axis (that is, up or down) with a value of 0.0, indicating that the top edge of the Document is currently aligned with the top edge of the window's content area.

因此,您需要做的是每次在循环中滚动时记录 y 的增加,对于循环的情况,您应该检查 pageYOffset == YScrolled .如果等于没有用户输入,否则有。

有个很好的例子here