Skip to content

Commit 10a5d50

Browse files
committed
ENH: Add scroll_element method.
1 parent 480cbe6 commit 10a5d50

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

botcity/web/bot.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,31 @@ def find_element(self, selector: str, by: str = By.CSS_SELECTOR, waiting_time=10
11871187
except (TimeoutException, NoSuchElementException):
11881188
return None
11891189

1190+
def scroll_element(self, element: WebElement, steps: int = 100, interval:float=500, start: int=0, end: int=None):
1191+
"""Scrolls down an element by its scroll height or a given amount defined by `start` and `end`.
1192+
1193+
This is useful for scrolling down a page to load more content or
1194+
to scroll down a dynamically loaded element.
1195+
1196+
Args:
1197+
element (WebElement): The element to scroll.
1198+
steps (int, optional): Number of steps in which to conclude the scroll. Defaults to 100.
1199+
interval (float, optional): Time interval between each step. Defaults to 500ms.
1200+
start (int, optional): Start position. Defaults to 0.
1201+
end (int, optional): End position. Defaults to None.
1202+
"""
1203+
ele_height = self.driver.execute_script(
1204+
"return arguments[0].scrollHeight;", element
1205+
)
1206+
1207+
start = max(0, start)
1208+
end = min(ele_height, end) if end is not None else ele_height
1209+
1210+
for i in range(start, end, steps):
1211+
self.driver.execute_script(
1212+
"arguments[0].scrollTo(0, arguments[1])", element, i)
1213+
self.sleep(interval/1000.0)
1214+
11901215
def wait_for_stale_element(self, element: WebElement, timeout: int = 10000):
11911216
"""
11921217
Wait until the WebElement element becomes stale (outdated).

0 commit comments

Comments
 (0)