1818import  socket 
1919import  requests 
2020import  psutil 
21+ import  pyperclip 
2122import  base64 , imghdr 
2223from  pathlib  import  Path 
2324from  urllib .parse  import  urlparse 
@@ -1650,6 +1651,7 @@ def Keystroke_For_Element(data_set):
16501651    # Parse the data set 
16511652    try :
16521653        stype  =  ""   # keys/chars 
1654+         paste_image  =  False  # identifiying image paste 
16531655        get_element  =  False   # Use element 
16541656        key_count  =  1   # Default number of button presses 
16551657        for  row  in  data_set :
@@ -1668,6 +1670,9 @@ def Keystroke_For_Element(data_set):
16681670                    keystroke_value  =  row [2 ]
16691671            elif  row [1 ] ==  "element parameter" :
16701672                get_element  =  True 
1673+             elif  row [1 ] ==  "optional parameter" :
1674+                 if  row [0 ] ==  "paste image" :
1675+                     paste_image  =  row [2 ].strip ().lower () ==  "true" 
16711676
16721677        if  stype  ==  "" :
16731678            CommonUtil .ExecLog (sModuleInfo , "Field contains incorrect data" , 3 )
@@ -1731,36 +1736,63 @@ def Keystroke_For_Element(data_set):
17311736                ):
17321737                    paste_key  =  Keys .CONTROL 
17331738
1734-                 try :
1735-                     if  get_element :
1736-                         selenium_driver .execute_script ("arguments[0].focus();" , Element )
1737-                         time .sleep (0.1 )
1738- 
1739-                         # Perform paste using ActionChains 
1740-                         actions  =  ActionChains (selenium_driver )
1741-                         actions .key_down (paste_key , element = Element )
1742-                         actions .send_keys_to_element (Element , "v" )
1743-                         actions .key_up (paste_key , element = Element )
1744-                         actions .perform ()
1745-                     else :
1746-                         actions  =  ActionChains (selenium_driver )
1747-                         actions .key_down (paste_key )
1748-                         actions .send_keys ("v" )
1749-                         actions .key_up (paste_key )
1750-                         actions .perform ()
1751- 
1752-                     CommonUtil .ExecLog (
1753-                         sModuleInfo , "Paste command executed successfully" , 1 
1754-                     )
1755-                     return  "passed" 
1756-                 except  Exception  as  e :
1757-                     # Fallback to JavaScript if ActionChains fails 
1739+                 if  paste_image :
17581740                    try :
1741+                         if  get_element :
1742+                             selenium_driver .execute_script ("arguments[0].focus();" , Element )
1743+                             time .sleep (0.1 )
1744+ 
1745+                             # Perform paste using ActionChains 
1746+                             actions  =  ActionChains (selenium_driver )
1747+                             actions .key_down (paste_key , element = Element )
1748+                             actions .send_keys_to_element (Element , "v" )
1749+                             actions .key_up (paste_key , element = Element )
1750+                             actions .perform ()
1751+                         else :
1752+                             actions  =  ActionChains (selenium_driver )
1753+                             actions .key_down (paste_key )
1754+                             actions .send_keys ("v" )
1755+                             actions .key_up (paste_key )
1756+                             actions .perform ()
1757+ 
17591758                        CommonUtil .ExecLog (
1760-                             sModuleInfo ,
1761-                             f"Standard paste failed: { str (e )}  ,
1762-                             2 ,
1759+                             sModuleInfo , "Copied image is successfully pasted" , 1 
17631760                        )
1761+                         return  "passed" 
1762+                     except  Exception  as  e :
1763+                         # Fallback to JavaScript if ActionChains fails 
1764+                         try :
1765+                             CommonUtil .ExecLog (
1766+                                 sModuleInfo ,
1767+                                 f"Standard paste failed. Trying JavaScript fallback..." ,
1768+                                 2 ,
1769+                             )
1770+                             if  get_element :
1771+                                 selenium_driver .execute_script (
1772+                                     "arguments[0].focus();" , Element 
1773+                                 )
1774+                                 selenium_driver .execute_script (
1775+                                     "arguments[0].value = arguments[1];" ,
1776+                                     Element ,
1777+                                     pyperclip .paste (),
1778+                                 )
1779+                             else :
1780+                                 selenium_driver .execute_script (
1781+                                     f"document.activeElement.value += '{ pyperclip .paste ()}  
1782+                                 )
1783+                             CommonUtil .ExecLog (
1784+                                 sModuleInfo , "Successfully image paste executed via JavaScript fallback" , 1 
1785+                             )
1786+                             return  "passed" 
1787+                         except  Exception  as  js_e :
1788+                             return  CommonUtil .Exception_Handler (
1789+                                 sys .exc_info (),
1790+                                 None ,
1791+                                 f"Both methods failed for paste the image: { str (js_e )}  ,
1792+                             )
1793+                 else :
1794+                     # simple text paste 
1795+                     try :
17641796                        if  get_element :
17651797                            selenium_driver .execute_script (
17661798                                "arguments[0].focus();" , Element 
@@ -1775,14 +1807,12 @@ def Keystroke_For_Element(data_set):
17751807                                f"document.activeElement.value += '{ pyperclip .paste ()}  
17761808                            )
17771809                        CommonUtil .ExecLog (
1778-                             sModuleInfo , "Paste executed via JavaScript fallback " , 1 
1810+                             sModuleInfo , "Paste successfully  executed via JavaScript" , 1 
17791811                        )
17801812                        return  "passed" 
17811813                    except  Exception  as  js_e :
1782-                         return  CommonUtil .Exception_Handler (
1783-                             sys .exc_info (),
1784-                             None ,
1785-                             f"Both methods failed for paste operation: { str (js_e )}  ,
1814+                         CommonUtil .ExecLog (
1815+                             sModuleInfo , "JavaScript paste execution failed. Trying keypress..." , 3 
17861816                        )
17871817
17881818            elif  "+"  in  keystroke_value :
0 commit comments