如何使用watir-scroll

问题描述

我有一个应用程序,其中有一个动态表,仅当您向上或向下滚动时才加载行。 Watir-scroll正在滚动整个页面。无论如何,我可以在该表中执行滚动吗?

解决方法

使元素可滚动通常是通过设置overflow样式来完成的。它可能在包含div的{​​{1}}上。例如:

table

Watir中没有内置的方法(至少从v6.17.0起)来滚动元素。但是,仍然有一些解决方法。

设置scrollTop

您可以通过设置元素的<html> <body> <div style="overflow:scroll; height:250px;"> <table> <tr height="200px"><td>Cell A</td></tr> <tr height="200px"><td>Cell B</td></tr> <tr height="200px"><td>Cell C</td></tr> <tr height="200px"><td>Cell D</td></tr> </table> </div> </body> </html> 属性来设置元素的滚动位置:

scrollTop

发送密钥

根据应用程序侦听滚动事件的方式,设置# Get the element that has the overflow property div = browser.div # Scroll to a specific point div.execute_script('arguments[0].scrollTop = 100;',div) # Scroll down a certain amount div.execute_script('arguments[0].scrollTop += 50;',div) 可能不会触发行的加载。一种更可能被检测到的方法是发送scrollTop:down键盘键-即更像真实用户。

Watir和Selenium-WebDriver似乎都禁止为此使用:page_down(引发不可交互的错误),因此您需要使用操作构建器:

#send_keys