[removed] How to detect if browser window is scrolled to bottom?

后端 未结 19 945
你的背包
你的背包 2020-11-22 16:00

I need to detect if a user is scrolled to the bottom of a page. If they are at the bottom of the page, when I add new content to the bottom, I will automatically scroll them

19条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 16:26

    I had to come up with a way (in Java) to systematically scroll down looking for a component for which I didn't know the correct XPath (long story, so just play along). As I just stated, I needed to scroll down while looking for a component and stop either when the component was found or the bottom of the page was reached.

    The following code snippet controls the scrolling down to the bottom of the page:

    JavascriptExecutor js = (JavascriptExecutor) driver;
    boolean found = false;
    long currOffset = 0;
    long oldOffset = 0;
    do
    {
        oldOffset = currOffset;
        // LOOP to seek the component using several xpath regexes removed
        js.executeScript("window.scrollBy(0, 100)");
        currOffset = (Long)js.executeScript("var offset = window.window.pageYOffset; return offset;");
    } while (!found && (currOffset != oldOffset));
    

    By the way, the window is maximized before this code snippet is executed.

提交回复
热议问题