[英]How to scroll within a table using watir-scroll
我有一個應用程序,其中有一個動態表,只有在向上或向下滾動時才會加載行。 Watir-scroll 正在滾動整個頁面。 無論如何,我可以在該表中執行滾動嗎?
使元素可滾動通常是通過設置overflow
樣式來完成的。 它很可能在包含table
的div
上。 例如:
<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
或: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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.