Skip to content

Commit aa259bb

Browse files
committed
Update scrolling methods
1 parent 42d5e11 commit aa259bb

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,7 @@ def uc_open_with_cdp_mode(driver, url=None, **kwargs):
898898
cdp.assert_not_in = CDPM.assert_not_in
899899
cdp.scroll_into_view = CDPM.scroll_into_view
900900
cdp.scroll_to_y = CDPM.scroll_to_y
901+
cdp.scroll_by_y = CDPM.scroll_by_y
901902
cdp.scroll_to_top = CDPM.scroll_to_top
902903
cdp.scroll_to_bottom = CDPM.scroll_to_bottom
903904
cdp.scroll_up = CDPM.scroll_up

seleniumbase/core/sb_cdp.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2700,6 +2700,13 @@ def scroll_to_y(self, y):
27002700
self.loop.run_until_complete(self.page.evaluate(js_code))
27012701
self.loop.run_until_complete(self.page.wait())
27022702

2703+
def scroll_by_y(self, y):
2704+
y = int(y)
2705+
js_code = "window.scrollBy(0, %s);" % y
2706+
with suppress(Exception):
2707+
self.loop.run_until_complete(self.page.evaluate(js_code))
2708+
self.loop.run_until_complete(self.page.wait())
2709+
27032710
def scroll_to_top(self):
27042711
js_code = "window.scrollTo(0, 0);"
27052712
with suppress(Exception):
@@ -2713,11 +2720,21 @@ def scroll_to_bottom(self):
27132720
self.loop.run_until_complete(self.page.wait())
27142721

27152722
def scroll_up(self, amount=25):
2716-
self.loop.run_until_complete(self.page.scroll_up(amount))
2723+
"""Scrolls up as a percentage of the page."""
2724+
try:
2725+
self.loop.run_until_complete(self.page.scroll_up(amount))
2726+
except Exception:
2727+
amount = self.get_window_size()["height"] * amount / 100
2728+
self.execute_script("window.scrollBy(0, -%s);" % amount)
27172729
self.loop.run_until_complete(self.page.wait())
27182730

27192731
def scroll_down(self, amount=25):
2720-
self.loop.run_until_complete(self.page.scroll_down(amount))
2732+
"""Scrolls down as a percentage of the page."""
2733+
try:
2734+
self.loop.run_until_complete(self.page.scroll_down(amount))
2735+
except Exception:
2736+
amount = self.get_window_size()["height"] * amount / 100
2737+
self.execute_script("window.scrollBy(0, %s);" % amount)
27212738
self.loop.run_until_complete(self.page.wait())
27222739

27232740
def save_page_source(self, name, folder=None):

seleniumbase/fixtures/base_case.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6523,6 +6523,34 @@ def scroll_to_y(self, y):
65236523
self.execute_script(scroll_script)
65246524
time.sleep(0.012)
65256525

6526+
def scroll_by_y(self, y):
6527+
"""Scrolls page by y pixels."""
6528+
self.__check_scope()
6529+
y = int(y)
6530+
if self.__is_cdp_swap_needed():
6531+
self.cdp.scroll_by_y(y)
6532+
return
6533+
scroll_script = "window.scrollBy(0, %s);" % y
6534+
with suppress(Exception):
6535+
self.execute_script(scroll_script)
6536+
time.sleep(0.012)
6537+
6538+
def scroll_up(self, amount=25):
6539+
"""Scrolls up as a percentage of the page."""
6540+
if self.__is_cdp_swap_needed():
6541+
self.cdp.scroll_up(amount)
6542+
return
6543+
amount = self.get_window_size()["height"] * amount / 100
6544+
self.execute_script("window.scrollBy(0, -%s);" % amount)
6545+
6546+
def scroll_down(self, amount=25):
6547+
"""Scrolls down as a percentage of the page."""
6548+
if self.__is_cdp_swap_needed():
6549+
self.cdp.scroll_down(amount)
6550+
return
6551+
amount = self.get_window_size()["height"] * amount / 100
6552+
self.execute_script("window.scrollBy(0, %s);" % amount)
6553+
65266554
def click_xpath(self, xpath):
65276555
"""Technically, self.click() automatically detects xpath selectors,
65286556
so self.click_xpath() is just a longer name for the same action."""

0 commit comments

Comments
 (0)