Mini-tip: Return a Javascript variable to Robin

Although lots of users will know this already, some less familiar with JS (like me) may not.

At some point you’ll want to get a value back from
WebAutomation.ExecuteJavascript

and the Action code itself gives you a big hint,

WebAutomation.ExecuteJavascript BrowserInstance: Javascript:'function ExecuteScript() { /*your code here, return something (optionally); */ }' Result=> Result

you might think you don’t have to use a ‘return’ statement in your JS (and maybe you don’t, but I haven’t had any luck without it).

Here’s a screenshot of my code:

and the code:

WebAutomation.LaunchChrome          Url: "https://www.startpage.com" \
                                    WindowState:WebAutomation.BrowserWindowState.Normal \
                                    ClearCache:False \
                                    ClearCookies:False \
                                    BrowserInstance=> Browser

wait 3

WebAutomation.ExecuteJavascript     BrowserInstance:  Browser \
                                    Javascript: \
                                    """
                                    function ExecuteScript()  
                                    {var a = document.querySelector(`title`).innerText; return a;}
                                    """ \ 
                                    Result=> Result # 'a' was returned by JS to 'Result'

Console.Write                       Message: Result

wait 1

WebAutomation.CloseWebBrowser       BrowserInstance: Browser

and the result.

result

Regards,
burque505

2 Likes