Skip to main content

Run-JS

Command Description

Run JS, and return results from JS run (string format)

Command Prototype

sRet = WebBrowser.RunJS(hWeb,sScript,bSync,optionArgs)

Parameter Description

ParameterRequiredTypeDefaultDescription
hWebTrueexpressionhWebBrowser handle string using WebBrowser.create or WebBrowser.bind Command Returns
sScriptTruestring"function(){return 123}"The content of JS script to be run
bSyncTruebooleanTrueWhether to run synchronously; if it is true, it will wait for the execution of JS to finish and then return and continue. If it is false, it will return immediately after the execution of JS
bContinueOnErrorNonebooleanNoneSpecify whether the automation should continue to support this field even if the activity is incorrect, it is only supported by the Boolean value (true, false) default value
iDelayAfterNonenumber300Delay time (in milliseconds) after executing the activity is 300 milliseconds
iDelayBeforeNonenumber200The delay time (in milliseconds) before any operations are executed is by default 200 milliseconds

return

sRet,The variable used to save the output of the command.

Demo

/************************************Execute JS************** ************************ 
Command prototype:
WebBrowser.RunJS(hWeb,sScript,bSync,optionArgs)
Input parameters:
hWeb--Browser object
sScript--JS script content
bSync--Whether to execute synchronously
bContinueOnError--whether to continue after error
iDelayAfter-- delay after execution
iDelayBefore--delay before execution
Outgoing reference:
sRet --The result returned by the command running
*********************************************************************************/
Dim hWeb, sRet
hWeb = WebBrowser.Create("ie","www.baidu.com",30000,{"bContinueOnError":false,"iDelayAfter":300,"iDelayBefore":200,"sBrowserPath":"","sStartArgs":""})
TracePrint(hWeb)
hWeb = WebBrowser.BindBrowser("ie",10000,{"bContinueOnError":false,"iDelayAfter":300,"iDelayBefore":200})
TracePrint(hWeb)
sRet = WebBrowser.RunJS(hWeb,"function(){return document.getElementsByClassName('bg s_btn')}",true,{"bContinueOnError":false,"iDelayAfter":300,"iDelayBefore":200})
TracePrint (sRet)