def execute_js(
javascript_code: str,
method: str = '',
timeout: int = 30
) -> str

Execute Javascript code for the target element.

Remarks: In the Javascript code, execute specified in parameter javascript_code, by using “_context$.currentElement.” for the target element.

 javascript_code[Required]: str
   Javascript code snippet to be executed upon target element.
 method: str
   The method to be invoked should be defined in the Javascript code. If any parameter needs to passed to the method, it can be included in this parameter value, for eg.: SetText(\”test\”).
 timeout: int
   Timeout for the operation, the unit is second, and the default value is 30 seconds.

 Str, execution result


from clicknium import clicknium as cc

# open chrome browser
chrome_tab = cc.chrome.open("https://www.bing.com")

# execute js, set text for target element
webEle = chrome_tab.find_element(locator.chrome.bing.search_sb_form_q)
js = "function SetText(text){_context$.currentElement.value = text; console.log(\"exit 0\"); return \"success\"}"

result = webEle.execute_js(js, "SetText(\"click\")")


What are your feelings
Updated on 29 August 2023