Skip to content

Commit 7a3fbbc

Browse files
committed
Better handling of page scrolling to elements
1 parent c5e79fc commit 7a3fbbc

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def click(self, selector, by=By.CSS_SELECTOR, timeout=None, delay=0):
133133
self.driver, selector, by, timeout=timeout)
134134
self.__demo_mode_highlight_if_active(selector, by)
135135
if not self.demo_mode:
136-
self.__scroll_to_element(element)
136+
self.__scroll_to_element(element, selector, by)
137137
pre_action_url = self.driver.current_url
138138
if delay and delay > 0:
139139
time.sleep(delay)
@@ -208,7 +208,7 @@ def double_click(self, selector, by=By.CSS_SELECTOR, timeout=None):
208208
self.driver, selector, by, timeout=timeout)
209209
self.__demo_mode_highlight_if_active(selector, by)
210210
if not self.demo_mode:
211-
self.__scroll_to_element(element)
211+
self.__scroll_to_element(element, selector, by)
212212
pre_action_url = self.driver.current_url
213213
try:
214214
actions = ActionChains(self.driver)
@@ -297,7 +297,7 @@ def update_text(self, selector, new_value, by=By.CSS_SELECTOR,
297297
selector, by=by, timeout=timeout)
298298
self.__demo_mode_highlight_if_active(selector, by)
299299
if not self.demo_mode:
300-
self.__scroll_to_element(element)
300+
self.__scroll_to_element(element, selector, by)
301301
try:
302302
element.clear()
303303
except (StaleElementReferenceException, ENI_Exception):
@@ -368,7 +368,7 @@ def add_text(self, selector, text, by=By.CSS_SELECTOR, timeout=None):
368368
selector, by=by, timeout=timeout)
369369
self.__demo_mode_highlight_if_active(selector, by)
370370
if not self.demo_mode:
371-
self.__scroll_to_element(element)
371+
self.__scroll_to_element(element, selector, by)
372372
pre_action_url = self.driver.current_url
373373
try:
374374
if not text.endswith('\n'):
@@ -1797,13 +1797,13 @@ def scroll_to(self, selector, by=By.CSS_SELECTOR, timeout=None):
17971797
element = self.wait_for_element_visible(
17981798
selector, by=by, timeout=timeout)
17991799
try:
1800-
self.__scroll_to_element(element)
1800+
self.__scroll_to_element(element, selector, by)
18011801
except (StaleElementReferenceException, ENI_Exception):
18021802
self.wait_for_ready_state_complete()
18031803
time.sleep(0.05)
18041804
element = self.wait_for_element_visible(
18051805
selector, by=by, timeout=timeout)
1806-
self.__scroll_to_element(element)
1806+
self.__scroll_to_element(element, selector, by)
18071807

18081808
def slow_scroll_to(self, selector, by=By.CSS_SELECTOR, timeout=None):
18091809
''' Slow motion scroll to destination '''
@@ -1846,7 +1846,7 @@ def js_click(self, selector, by=By.CSS_SELECTOR, all_matches=False):
18461846
if self.is_element_visible(selector, by=by):
18471847
self.__demo_mode_highlight_if_active(selector, by)
18481848
if not self.demo_mode:
1849-
self.__scroll_to_element(element)
1849+
self.__scroll_to_element(element, selector, by)
18501850
css_selector = self.convert_to_css_selector(selector, by=by)
18511851
css_selector = re.escape(css_selector)
18521852
css_selector = self.__escape_quotes_if_needed(css_selector)
@@ -4047,8 +4047,12 @@ def __demo_mode_highlight_if_active(self, selector, by):
40474047
selector, by=by, timeout=settings.SMALL_TIMEOUT)
40484048
self.__slow_scroll_to_element(element)
40494049

4050-
def __scroll_to_element(self, element):
4051-
js_utils.scroll_to_element(self.driver, element)
4050+
def __scroll_to_element(self, element, selector=None, by=By.CSS_SELECTOR):
4051+
success = js_utils.scroll_to_element(self.driver, element)
4052+
if not success and selector:
4053+
self.wait_for_ready_state_complete()
4054+
element = page_actions.wait_for_element_visible(
4055+
self.driver, selector, by, timeout=settings.SMALL_TIMEOUT)
40524056
self.__demo_mode_pause_if_active(tiny=True)
40534057

40544058
def __slow_scroll_to_element(self, element):

seleniumbase/fixtures/js_utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,8 @@ def scroll_to_element(driver, element):
640640
try:
641641
element_location = element.location['y']
642642
except Exception:
643-
element.location_once_scrolled_into_view
644-
return
643+
# element.location_once_scrolled_into_view # Old hack
644+
return False
645645
element_location = element_location - 130
646646
if element_location < 0:
647647
element_location = 0
@@ -650,8 +650,9 @@ def scroll_to_element(driver, element):
650650
# scroll_script = "jQuery('%s')[0].scrollIntoView()" % selector
651651
try:
652652
driver.execute_script(scroll_script)
653-
except WebDriverException:
654-
pass # Older versions of Firefox experienced issues here
653+
return True
654+
except Exception:
655+
return False
655656

656657

657658
def slow_scroll_to_element(driver, element, browser):

0 commit comments

Comments
 (0)