简体   繁体   English

只要有更多内容要显示,Watir就会向下滚动页面

[英]Watir scroll down the page as long as there is more content to show

I want to scroll down the page as long as there is more content to show. 只要要显示更多内容,我就想向下滚动页面。

For example, when you click followers on Instagram, a dialog window will pop up and it will load more followers as you scroll down as long as there are more followers to show. 例如,当您单击Instagram上的关注者时,将弹出一个对话框,当您向下滚动时,它将显示更多关注者,只要要显示更多关注者即可。

Instagram追随者对话框窗口

I could do this the hard way like getting the follower count and then counting the followers as I scroll down and when they equal stop scrolling there. 我可以用困难的方式做到这一点,例如获取关注者人数,然后在我向下滚动时以及当它们相等时停止滚动到那里时对关注者进行计数。

But I wanted to ask you whether there could be a better way. 但是我想问你是否有更好的方法。

You can check if a scrollable element is at the bottom by comparing its scrollTop and scrollHeight. 您可以通过比较其scrollTop和scrollHeight来检查可滚动元素是否在底部。

The following work: 以下工作:

scrollable = browser.div(class: 'j6cq2') # div with overflow-y=scroll
until browser.execute_script('return arguments[0].scrollTop + arguments[0].clientHeight >= arguments[0].scrollHeight', scrollable) do
    browser.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', scrollable)
    sleep(1)
end

Note that I didn't have time to find a better solution than using #sleep. 请注意,我没有时间比使用#sleep来找到更好的解决方案。 However, hopefully this gives you an idea. 但是,希望这能给您一个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM