Skip to main content

As it is, it is a struggle to run basic code in Browser: Run Javascript action.

For example, running a built in function normally executes its intended functionality without error: 

  • document.getElementById(“id”).click() //executes a click on the page fine
  • alert(“hello”) //signals the page to popup with “hello”

However, variables and loops do not work:

  • let x = “hi”; alert(x)
  • for(int i = 0; i < 10; i++){}

These delay as long as the Browser: Run Javascript action was given in its delay field, then error with “Could not execute chrome request within the supplied timeout”. For reference, a normal error such as getting an element that doesn’t exist and clicking on that non-existent element errors instantly with a seperate error message.
 

Alone these constraints are a nuisance, but can be designed around with using the loop action and variables. However, when my bot was switched to a different computer, it failed to run “document.querySelector(‘ndata-automationid=”uploadCommand”]’).click()) with the same error. I am unable to use recorder capture to click on this element, I must use javascript, so this is a major issue. I stopped being able to run these built in functions to interact with the webpage.

I am wondering if the Browser: Run Javascript action is functioning like this for everyone or if it is the way automation anywhere is setup, along with the chrome extension, version of chrome, etc. Does anyone know how to get rid of the “Could not execute chrome request within the supplied timeout”?

Thanks a lot

@Jack Hengen Here’s what I found: (I’m using the sample page https://rpademo.automationanywhere.com/ if you want to follow along.)

Example 1: document.getElementById("username").value = "Aaron";

This works. It fills in the Username field with Aaron. So far so good.

Example 2: document.querySelector('Sdata-automationid="uploadCommand"]').click();

This also works. Yes, I added that custom tag to my button for testing. Did you have an extra parenthesis at the end?

Example 3: alert(“Hello world.”);

This works. Easy peasy.

Example 4: var greeting = “Hello world.”; alert(greeting);

This does not work. To understand why, we need to look at the handler. (I picked this up from the Developer Tools within Chrome.)

Can you see why the var greeting didn’t work? Look at line 4. Because var is not a function. (Neither is “let” in your example above.) 

To make this work, we need to enclose this into a self-executing function. Weird, yes, but it solves the handler issue.

(function () {
var greeting = "Hello world.";
alert(greeting);
})();

This works.

Example 5: for (int i = 1; i < 4; i++) { alert(i); }

First, JavaScript does not like the “int” designation to initialize a variable. Changing that to a “var” or “let” works.

Now, based on what the handler is expecting, we know that “for” isn’t a function, so this will fail even with the correct variable definition. However, if we enclose it in the self-execution function, it works.

(function () {
for (var i = 1; i < 4; i++) {
alert(i);
}
})();

We can use the return statement to return any value we want back to returnResult in the handler.

Hope this helps!


Hi ​@Aaron.Gleason thanks for the quick response! The extra “)” and the “int” syntax errors were not in the original code, just in my write up of the issue, sorry about that. I see I can get the functionality I want by wrapping the code in a function. However I am a little confused about the screenshot you uploaded.

Is this screenshot from chrome dev tools and how it handles running javascript in the console or is it from automation anywhere and how it injects the code into the console? Is the everything between “var returnResult =” and “if(returnResult...” the code written in Browser: Run Javascript? Is there anyway you can link me to where you found the screenshot/code snippet?

I get that code structured like “var x = var y = someval;” shouldn’t work, but why does the error indicate a time out?

Thanks a lot!


@Jack Hengen The handler is code that we are injecting into the website, which contains your code from the Run JavaScript action.

The timeout is happening because the handler code is causing an error. This causes Chrome not to return control back to the automation. If you open the Developer Tools (F12) in Chrome and watch the errors happen in the Console, the errors point to that handler, which is how I figured out what was wrong.

To see the handler yourself, open the Developer Tools and navigate to the Console. Run the old code (that caused the error) and you will see something like this:

Clicking on the last link (highlighted) brings up the raw JavaScript function we inject to the page (at top).

All the while the bot agent is waiting for a return from the browser… which never comes… producing the timeout you see.


Perfect! Thanks so much for your help!


Reply