@@ -3530,14 +3530,34 @@ def copy_image_into_browser(data_set):
35303530 # Convert
35313531 image_b64 = base64 .b64encode (image_data ).decode ('utf-8' )
35323532
3533- async_script = """
3534- const base64Data = arguments[0];
3535- const mimeType = arguments[1];
3536- const callback = arguments[arguments.length - 1];
3537-
3538- function b64toBlob(b64Data, contentType) {
3539- contentType = contentType || 'image/png';
3540- const byteCharacters = atob(b64Data);
3533+ browser_name = selenium_driver .capabilities .get ('browserName' , '' ).lower ()
3534+ if browser_name in ('chrome' , 'microsoft edge' , 'edge' ):
3535+ try :
3536+ selenium_driver .execute_cdp_cmd ('Browser.setClipboard' , {
3537+ 'data' : image_b64 ,
3538+ 'type' : mime_type
3539+ })
3540+ CommonUtil .ExecLog (sModuleInfo , f"Image copied to clipboard via CDP: { image_path } " , 1 )
3541+ return "passed"
3542+ except Exception as e :
3543+ CommonUtil .ExecLog (sModuleInfo , f"CDP failed ({ str (e )} ). Trying fallback method" , 2 )
3544+
3545+ try :
3546+ # Grant clipboard permissions via CDP if possible
3547+ try :
3548+ from urllib .parse import urlparse
3549+ parsed_uri = urlparse (selenium_driver .current_url )
3550+ origin = f'{ parsed_uri .scheme } ://{ parsed_uri .netloc } '
3551+ selenium_driver .execute_cdp_cmd ('Browser.grantPermissions' , {
3552+ 'origin' : origin ,
3553+ 'permissions' : ['clipboardReadWrite' , 'clipboardSanitizedWrite' ]
3554+ })
3555+ except :
3556+ pass
3557+
3558+ async_script = """
3559+ const [base64Data, mimeType, callback] = arguments;
3560+ const byteCharacters = atob(base64Data);
35413561 const byteArrays = [];
35423562
35433563 for (let offset = 0; offset < byteCharacters.length; offset += 512) {
@@ -3548,56 +3568,26 @@ def copy_image_into_browser(data_set):
35483568 byteNumbers[i] = slice.charCodeAt(i);
35493569 }
35503570
3551- const byteArray = new Uint8Array(byteNumbers);
3552- byteArrays.push(byteArray);
3571+ byteArrays.push(new Uint8Array(byteNumbers));
35533572 }
35543573
3555- return new Blob(byteArrays, {type: contentType});
3556- }
3557-
3558- const blob = b64toBlob(base64Data, mimeType);
3559- const item = new ClipboardItem({ [mimeType]: blob });
3560-
3561- navigator.clipboard.write([item])
3562- .then(() => callback(true))
3563- .catch(async (err) => {
3564- console.error('Standard clipboard failed:', err);
3565- callback(false);
3566- });
3567- """
3568-
3569- success = selenium_driver .execute_async_script (async_script , image_b64 , mime_type )
3570- if success :
3571- CommonUtil .ExecLog (sModuleInfo , f"The image ({ mime_type } ) successfully copied to clipboard." , 1 )
3572- return "passed"
3573- else :
3574- CommonUtil .ExecLog (sModuleInfo , "Failed to write image to clipboard" , 3 )
3574+ const blob = new Blob(byteArrays, {type: mimeType});
3575+ const item = new ClipboardItem({ [mimeType]: blob });
3576+
3577+ navigator.clipboard.write([item])
3578+ .then(() => callback(true))
3579+ .catch(err => callback(false));
3580+ """
3581+
3582+ success = selenium_driver .execute_async_script (async_script , image_b64 , mime_type )
3583+ if success :
3584+ CommonUtil .ExecLog (sModuleInfo , f"Image copied to clipboard: { image_path } " , 1 )
3585+ return "passed"
35753586 return "zeuz_failed"
3576-
3577- ################################## PASTE ##################################
3578- # capabilities = selenium_driver.capabilities
3579- # platform_name = capabilities.get('platformName', '').lower()
3580- # browser_name = capabilities.get('browserName', '').lower()
3581-
3582- # if 'mac' in platform_name or 'os x' in platform_name:
3583- # paste_key = Keys.COMMAND
3584- # elif platform.system().lower() in ('darwin', 'macos'):
3585- # paste_key = Keys.COMMAND
3586- # else:
3587- # paste_key = Keys.CONTROL
3588-
3589- # # handling for Firefox on Linux
3590- # if browser_name == 'firefox' and ('linux' in platform_name or platform.system().lower() == 'linux'):
3591- # paste_key = Keys.CONTROL
3592-
3593- # try:
3594- # ActionChains(selenium_driver).key_down(paste_key).send_keys('v').key_up(paste_key).perform()
3595- # except Exception as e:
3596- # CommonUtil.ExecLog(sModuleInfo, f"Standard paste failed: {str(e)}. Trying JavaScript fallback...", 2)
3597- # selenium_driver.execute_script("document.execCommand('paste');")
3598-
3599- # CommonUtil.ExecLog(sModuleInfo, f"Image ({mime_type}) pasted successfully", 1)
3600- # return "passed"
3601-
3587+
3588+ except Exception as e :
3589+ CommonUtil .ExecLog (sModuleInfo , f"Fallback method failed: { str (e )} " , 3 )
3590+ return "zeuz_failed"
3591+
36023592 except Exception :
3603- return CommonUtil .Exception_Handler (sys .exc_info ())
3593+ return CommonUtil .Exception_Handler (sys .exc_info ())
0 commit comments