使元素可滚动通常是通过设置overflow
风格。它很可能在一个div
其中包含table
。例如:
<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>
Watir 中(至少从 v6.17.0 开始)没有用于滚动元素的内置方法。然而,仍然有一些解决方法。
设置滚动顶部
您可以通过设置元素的滚动位置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
or :page_down
键盘按键 - 即更像真正的用户。
看起来 Watir 和 Selenium-WebDriver 都阻止使用#send_keys
为此(引发不可交互的错误),因此您需要使用操作生成器:
# Get the element that has the overflow property
div = browser.div
# Scroll down a bit
browser.wd.action.send_keys(div.wd, :down).perform
browser.wd.action.send_keys(div.wd, :page_down).perform
# Scroll to the bottom
browser.wd.action.send_keys(div.wd, :end).perform