Search code examples
blueprismrpa

Unable to Invoke Inserted Javascript Fragment


I am attempting to insert a Javascript fragment into a webpage and then invoke it using blue prism. The purpose of this is to analyse what elements are returned from a search to determine where to go next in the overall process flow.

I have tested the Javascript code on the intended website using the IE 11 developer console and it works without issue. The code is below in case it is useful.

function includes(stringToCheck, CharacterToSearchFor)
{
    var found = new Boolean();
    var splitString = stringToCheck.split("");

    for (var index = 0; index < splitString.length; index++) 
    {
        if(splitString[index] == CharacterToSearchFor)
        {
            return true;
        }
    }
    return false;
}

function getPartners() //declare a function which can be called from BP. once called all code within the enclosing {} will be run
{
    var searchResults = document.getElementsByClassName("findASolicitorListItem"); //search the web page for all elements with a specific tag and store them in a variable called searchResults.

    if(searchResults.length == 0) // If the number 
    {
        alert( "No Solicitors were found.");
    }else if(searchResults.length == 1)
    {
        var innerSearchResults = searchResults[0].getElementsByTagName("span");

        for(i = 0; i < innerSearchResults.length; i++)
        {
            var spanText = innerSearchResults[i].innerText.toString();

            if((spanText != ""))
            {
                if(!includes(spanText, "|"))
                {
                    alert("One Solicitor found. " + spanText);
                }
            }
        }

    }else if (searchResults.length > 1)
    {
        alert( "More than one solicitor was found. Manual Checking required.");
    }
}

This is stored in a data item and is passed into the Navigate stage (Insert Javascript Fragment) parameter.

PrintScreen of Insert Javascript Fragment stage

When this stage is ran it successfully injects the Javascript functions into the webpage.

I then try and invoke this inserted javascript fragment

Printscreen of Invoke Javascript Function stage

When this stage runs I get the following error message raised by Blue Prism.

Internal : Failed to perform step 1 in Navigate Stage 'Analyse Result' on page 'Analyse Search Results' - Failed while invoking javascript method:Exception from HRESULT: 0x80020101-> at mshtml.HTMLWindow2Class.IHTMLWindow2_execScript(String code, String language) at BluePrism.ApplicationManager.HTML.clsHTMLDocument.InvokeJavascriptMethod(String methodname, String jsonargs, Object& retval, String& sErr)

I have searched for this error code and found this answer which indicates there is a problem with the code however I can manually run this code just fine.

Does anyone have any experience with using these methods in BluePrism or have seen this error message before who can help me resolve?


Solution

  • To invoke function by action "Invoke Javascript function", in Arguments field you should put arguments in JSON syntax. If there is no argument you put "[{}]". enter image description here

    On the above Marek's example the function should look:

    function sayHello(name)
    {
        alert("Hello " + name.name + "!");
    }
    

    and arguments: "[{'name':'world'}]".