如何使用 watir-scroll 在表格内滚动 [英] How to scroll within a table using watir-scroll
问题描述
我有一个应用程序,其中有一个动态表,只有在向上或向下滚动时才会加载行.Watir-scroll 正在滚动整个页面.无论如何,我可以在该表中执行滚动吗?
使元素可滚动通常是通过设置 overflow
样式来完成的.它可能位于包含 table
的 div
上.例如:
<身体><div style="overflow:scroll;高度:250px;"><表格><tr height=200px"><td>单元格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>
</html>
Watir(至少从 v6.17.0 开始)没有用于滚动元素的内置方法.但是,仍然有一些解决方法.
设置滚动顶部
你可以通过设置元素的scrollTop
属性来设置元素的滚动位置:
# 获取具有溢出属性的元素div = 浏览器.div# 滚动到特定点div.execute_script('arguments[0].scrollTop = 100;', div)# 向下滚动一定数量div.execute_script('arguments[0].scrollTop += 50;', div)
发送密钥
根据您的应用程序侦听滚动事件的方式,设置 scrollTop
可能不会触发行的加载.一种更有可能被检测到的方法是发送 :down
或 :page_down
键盘键 - 即更像真实用户.
看起来 Watir 和 Selenium-WebDriver 都禁止为此使用 #send_keys
(抛出不可交互的错误),因此您需要使用操作构建器:
# 获取具有溢出属性的元素div = 浏览器.div# 向下滚动一点browser.wd.action.send_keys(div.wd, :down).performbrowser.wd.action.send_keys(div.wd, :page_down).perform# 滚动到底部browser.wd.action.send_keys(div.wd, :end).perform
I have an application in which there is a dynamic table, rows are loaded only when you scroll up or down. Watir-scroll is scrolling the entire page. Is there anyway I can perform the scrolling within that table?
Making an element scrollable is often done by setting the overflow
style. It is likely on a div
that contains the table
. For example:
<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>
There are no built-in methods in Watir (at least as of v6.17.0) for scrolling an element. However, there are still some workarounds.
Set scrollTop
You can set the scroll position of the element by setting its scrollTop
property:
# 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)
Send Keys
Depending how your application is listening for the scrolled event, setting the scrollTop
might not trigger the loading of rows. An approach that is more likely to be detected is by sending the :down
or :page_down
keyboard keys - ie more like a real user.
It looks like both Watir and Selenium-WebDriver prevent using #send_keys
for this (throws not interactable errors), so you'll need to use the action builder:
# 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
这篇关于如何使用 watir-scroll 在表格内滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!