@@ -3158,6 +3158,18 @@ def Sleep(step_data):
31583158 return CommonUtil .Exception_Handler (sys .exc_info ())
31593159
31603160
3161+ def generate_scroll_offset (direction : str , pixel : int ) -> str :
3162+ if direction == "down" :
3163+ return f"0,{ pixel } "
3164+ elif direction == "up" :
3165+ return f"0,-{ pixel } "
3166+ elif direction == "left" :
3167+ return f"-{ pixel } ,0"
3168+ elif direction == "right" :
3169+ return f"{ pixel } ,0"
3170+ else :
3171+ return "0,0"
3172+
31613173# Method to scroll down a page
31623174@logger
31633175def Scroll (step_data ):
@@ -3181,14 +3193,7 @@ def Scroll(step_data):
31813193 if get_element :
31823194 Element = LocateElement .Get_Element (step_data , selenium_driver )
31833195
3184- if scroll_direction == "down" :
3185- offset = f"0,{ pixel } "
3186- elif scroll_direction == "up" :
3187- offset = f"0,-{ pixel } "
3188- elif scroll_direction == "left" :
3189- offset = f"-{ pixel } ,0"
3190- elif scroll_direction == "right" :
3191- offset = f"{ pixel } ,0"
3196+ offset = generate_scroll_offset (scroll_direction , pixel )
31923197
31933198 CommonUtil .ExecLog (sModuleInfo , f"Scrolling { scroll_direction } " , 1 )
31943199 selenium_driver .execute_script (f"{ 'arguments[0]' if Element is not None else 'window' } .scrollBy({ offset } )" )
@@ -3203,16 +3208,41 @@ def Scroll(step_data):
32033208@logger
32043209def scroll_to_element (step_data ):
32053210 sModuleInfo = inspect .currentframe ().f_code .co_name + " : " + MODULE_NAME
3206- global selenium_driver
3207- use_js = False
3211+ retur_ntop_left_script = '''
3212+ var Top = window.pageYOffset || document.documentElement.scrollTop;
3213+ var Left = window.pageXOffset || document.documentElement.scrollLeft;
3214+ return [Top, Left]'''
3215+ method = "js"
32083216 additional_scroll = 0.1
3217+ direction = ""
3218+ top , left = None , None
3219+ alignToTop = 'true'
3220+ '''
3221+ The alignToTop parameter is a boolean value that can be either true or false. Here's what each value does:
3222+
3223+ true: If alignToTop is set to true, the browser will scroll the element so that it is positioned at the
3224+ top of the visible area of the window. In other words, the top of the element will be aligned with the top of the viewport.
3225+
3226+ false (or omitted): If alignToTop is set to false or omitted, the browser will scroll the element into the
3227+ visible area of the window, but it will try to align the bottom of the element with the bottom of the viewport.
3228+ This means that the element will be positioned at the bottom of the visible area.
3229+ '''
3230+
32093231 try :
32103232 for left , mid , right in step_data :
32113233 left = left .lower ().strip ()
3234+ right = right .strip ().lower ()
32123235 if "use js" == left :
3213- use_js = right .strip ().lower () in ("true" , "yes" , "1" )
3214- elif "additional scroll" == left :
3215- additional_scroll = float (right .strip ())
3236+ method = "js" if right in ("true" , "yes" , "1" ) else "action chain"
3237+ elif "align to top" == left :
3238+ alignToTop = "true" if right in ("true" , "yes" , "1" ) else "false"
3239+ elif "method" == left :
3240+ method = right
3241+ elif "additional scroll" in left :
3242+ additional_scroll = float (right )
3243+ d = right .lstrip ("additional scroll" ).strip ()
3244+ if d in ("up" , "down" , "left" , "right" ):
3245+ direction = d
32163246
32173247 scroll_element = LocateElement .Get_Element (step_data , selenium_driver )
32183248 if scroll_element in failed_tag_list :
@@ -3221,18 +3251,56 @@ def scroll_to_element(step_data):
32213251 )
32223252 return "zeuz_failed"
32233253
3254+ if not direction and additional_scroll > 0 :
3255+ try :
3256+ top , left = selenium_driver .execute_script (retur_ntop_left_script )
3257+ except :
3258+ top , left = None , None
3259+
3260+ if method == "js" :
3261+ selenium_driver .execute_script (f"arguments[0].scrollIntoView({ alignToTop } );" , scroll_element )
3262+ elif method == "webdriver" :
3263+ scroll_element .location_once_scrolled_into_view
3264+ else :
3265+ ActionChains (selenium_driver ).move_to_element (scroll_element ).perform ()
3266+
32243267 CommonUtil .ExecLog (
32253268 sModuleInfo ,
3226- "Element to which instructed to scroll has been found. Scrolling to view it " ,
3269+ f"Scrolled to view with method = { method } " ,
32273270 1 ,
32283271 )
3229- if use_js :
3230- selenium_driver .execute_script ("arguments[0].scrollIntoView(true);" , scroll_element )
3231- else :
3232- ActionChains (selenium_driver ).move_to_element (scroll_element ).perform ()
32333272
3234- if additional_scroll > 0 :
3235- selenium_driver .execute_script (f"window.scrollBy(0,{ round (selenium_driver .get_window_size ()['height' ]* additional_scroll )} )" )
3273+ if not direction and additional_scroll > 0 and top is not None and left is not None :
3274+ try :
3275+ newTop , newLeft = selenium_driver .execute_script (retur_ntop_left_script )
3276+ if newTop > top :
3277+ direction = "down"
3278+ elif newTop < top :
3279+ direction = "up"
3280+ elif newLeft > left :
3281+ direction = "right"
3282+ elif newLeft < left :
3283+ direction = "left"
3284+ else :
3285+ direction = ""
3286+ except :
3287+ direction = ""
3288+
3289+ if (method in ["js" , "webdriver" ]) and \
3290+ (alignToTop == "true" and direction in ["down" , "right" ]) or \
3291+ (alignToTop == "false" and direction in ["up" , "left" ]):
3292+ # No need of default additional scroll
3293+ direction = ""
3294+
3295+ if direction and additional_scroll > 0 :
3296+ time .sleep (1 )
3297+ offset = generate_scroll_offset (direction , round (selenium_driver .get_window_size ()["height" if direction in ("up" , "down" ) else "width" ] * additional_scroll ))
3298+ CommonUtil .ExecLog (
3299+ sModuleInfo ,
3300+ f"Doing additional scroll in { direction } direction, { additional_scroll * 100 } % of { 'height' if direction in ('up' , 'down' ) else 'width' } of html body, ({ offset } ) pixels" ,
3301+ 1 ,
3302+ )
3303+ selenium_driver .execute_script (f"window.scrollBy({ offset } )" )
32363304 return "passed"
32373305
32383306 except Exception :
@@ -3241,22 +3309,15 @@ def scroll_to_element(step_data):
32413309
32423310# Method to scroll to view an element
32433311@logger
3244- def scroll_element_to_top (step_data ):
3312+ def scroll_to_top (step_data ):
32453313 sModuleInfo = inspect .currentframe ().f_code .co_name + " : " + MODULE_NAME
3246- global selenium_driver
32473314 try :
3248- scroll_element = LocateElement .Get_Element (step_data , selenium_driver )
3249- if scroll_element in failed_tag_list :
3250- CommonUtil .ExecLog (
3251- sModuleInfo , "Element to which instructed to scroll not found" , 3
3252- )
3253- return "zeuz_failed"
3315+ selenium_driver .execute_script (f"window.scroll(0,0)" )
32543316 CommonUtil .ExecLog (
32553317 sModuleInfo ,
3256- "Element to which instructed to scroll to top of the page has been found. Scrolling to view it at the top " ,
3318+ "Scrolled to top of the html " ,
32573319 1 ,
32583320 )
3259- scroll_element .location_once_scrolled_into_view
32603321 return "passed"
32613322
32623323 except Exception :
0 commit comments