Skip to content

Commit ae19caa

Browse files
committed
Add scroll_into_view(selector)
1 parent 6184e07 commit ae19caa

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

help_docs/method_summary.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,8 @@ self.scroll_to(selector, by="css selector", timeout=None)
359359
self.slow_scroll_to(selector, by="css selector", timeout=None)
360360
# Duplicates: self.slow_scroll_to_element(selector, by="css selector")
361361

362+
self.scroll_into_view(selector, by="css selector", timeout=None)
363+
362364
self.scroll_to_top()
363365

364366
self.scroll_to_bottom()

seleniumbase/fixtures/base_case.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5471,6 +5471,7 @@ def scroll_to(self, selector, by="css selector", timeout=None):
54715471
self.__scroll_to_element(element, selector, by)
54725472

54735473
def scroll_to_element(self, selector, by="css selector", timeout=None):
5474+
"""Same as self.scroll_to()"""
54745475
self.scroll_to(selector, by=by, timeout=timeout)
54755476

54765477
def slow_scroll_to(self, selector, by="css selector", timeout=None):
@@ -5508,8 +5509,21 @@ def slow_scroll_to(self, selector, by="css selector", timeout=None):
55085509
def slow_scroll_to_element(
55095510
self, selector, by="css selector", timeout=None
55105511
):
5512+
"""Same as self.slow_scroll_to()"""
55115513
self.slow_scroll_to(selector, by=by, timeout=timeout)
55125514

5515+
def scroll_into_view(self, selector, by="css selector", timeout=None):
5516+
"""Uses the JS scrollIntoView() method to scroll to an element.
5517+
Unlike other scroll methods, (which put elements upper-center),
5518+
this method places elements at the very top of the screen."""
5519+
self.__check_scope()
5520+
if not timeout:
5521+
timeout = settings.SMALL_TIMEOUT
5522+
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
5523+
timeout = self.__get_new_timeout(timeout)
5524+
element = self.wait_for_element_visible(selector, by, timeout=timeout)
5525+
self.execute_script("arguments[0].scrollIntoView();", element)
5526+
55135527
def scroll_to_top(self):
55145528
"""Scroll to the top of the page."""
55155529
self.__check_scope()

seleniumbase/fixtures/js_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,8 @@ def scroll_to_element(driver, element):
10391039
)
10401040
# The old jQuery scroll_script required by=By.CSS_SELECTOR
10411041
# scroll_script = "jQuery('%s')[0].scrollIntoView()" % selector
1042+
# This other scroll_script does not centralize the element
1043+
# driver.execute_script("arguments[0].scrollIntoView();", element)
10421044
try:
10431045
driver.execute_script(scroll_script)
10441046
return True

0 commit comments

Comments
 (0)