@@ -4886,11 +4886,13 @@ def get_offsets(location: str, Element: WebElement) -> tuple[int]:
48864886def drag_and_drop (dataset ):
48874887 sModuleInfo = inspect .currentframe ().f_code .co_name + " : " + MODULE_NAME
48884888 global selenium_driver
4889+ from time import sleep
48894890 try :
48904891 source = []
48914892 destination = []
48924893 destination_offset = None
4893- param_dict = {"elementparameter" : "element parameter" , "parentparameter" : "parent parameter" , "siblingparameter" : "sibling parameter" , "childparameter" : "child parameter" }
4894+ delay = None
4895+ param_dict = {"elementparameter" : "element parameter" , "parentparameter" : "parent parameter" , "siblingparameter" : "sibling parameter" , "childparameter" : "child parameter" ,"optionalparameter" : "optional parameter" }
48944896 for left , mid , right in dataset :
48954897 if mid .startswith ("src" ) or mid .startswith ("source" ):
48964898 mid = mid .replace ("src" , "" ).replace (" " , "" ).replace ("source" , "" )
@@ -4907,6 +4909,8 @@ def drag_and_drop(dataset):
49074909 destination .append ((left , mid , right ))
49084910 elif left .strip ().lower () == "destination offset" and mid .strip ().lower () == 'optional parameter' :
49094911 destination_offset = right
4912+ elif left .strip ().lower () == "delay" :
4913+ delay = int (right .strip ())
49104914
49114915 if not source :
49124916 CommonUtil .ExecLog (sModuleInfo , 'Please provide source element with "src element parameter", "src parent parameter" etc. Example:\n ' +
@@ -4928,7 +4932,54 @@ def drag_and_drop(dataset):
49284932 CommonUtil .ExecLog (sModuleInfo , "Destination Element is not found" , 3 )
49294933 return "zeuz_failed"
49304934
4931- if destination_offset :
4935+ if delay :
4936+ if destination_offset :
4937+ destination_x , destination_y = get_offsets (destination_offset , destination_element )
4938+ else :
4939+ destination_x = destination_element .location ['x' ]
4940+ destination_y = destination_element .location ['y' ]
4941+ distance_x = destination_x - source_element .location ['x' ]
4942+ distance_y = destination_y - source_element .location ['y' ]
4943+ total_time = delay
4944+ total_distance = (distance_x ** 2 + distance_y ** 2 )** 0.5
4945+
4946+ pixels_per_step = 5
4947+ steps = int (total_distance / pixels_per_step )
4948+
4949+ # Calculate the ideal time per step to fit within the total time
4950+ ideal_time_per_step = total_time / steps
4951+
4952+ # Start the high-resolution timer
4953+ start_time = time .perf_counter ()
4954+
4955+ # Create an ActionChains object
4956+ actions = ActionChains (selenium_driver )
4957+
4958+ # Click and hold the source element
4959+ actions .click_and_hold (source_element ).perform ()
4960+
4961+ # Manually move the mouse to the target element in small increments
4962+ for i in range (steps ):
4963+ # Calculate the movement for this step
4964+ move_x = distance_x / steps
4965+ move_y = distance_y / steps
4966+
4967+ # Move the mouse by the calculated offset
4968+ actions .move_by_offset (move_x , move_y ).perform ()
4969+
4970+ # Calculate elapsed time and adjust the sleep time
4971+ elapsed_time = time .perf_counter () - start_time
4972+ remaining_time = total_time - elapsed_time
4973+ time_per_step = remaining_time / (steps - i )
4974+
4975+ if time_per_step > 0 :
4976+ time .sleep (time_per_step )
4977+
4978+ # Release the mouse button to drop the element
4979+ actions .release ().perform ()
4980+ sleep (2 )
4981+
4982+ elif destination_offset :
49324983 x , y = get_offsets (destination_offset , destination_element )
49334984 ActionChains (selenium_driver ).click_and_hold (source_element ).move_to_element_with_offset (destination_element , x , y ).release ().perform ()
49344985 else :
0 commit comments