Execute Async Script arguments is empty

62 views
Skip to first unread message

Lucas Hamie

unread,
Feb 3, 2022, 10:20:24 PM2/3/22
to Selenium Users
Hi!

I'm using Selenium IDE on Windows on Chrome. 

I'm currently using a Execute Async Script command, that has as target the following code: ```console.log(arguments);``` 

That code is always printing an empty object, so when I try to grab arguments[0] to get the callback function I always get undefined.

I've already checked the docs and most stack overflow related questions I could find. 
 
Anything I might be missing? Is this a bug?

David

unread,
Feb 4, 2022, 5:35:29 PM2/4/22
to Selenium Users
What are you passing to the (java)script as "arguments"? I'm not familiar with using the functionality through Selenium IDE, but back when I used Selenium 2, executing javascript synchronously or asynchronously, arguments, or rather arguments[N] is used when you actually pass in arguments to the script, otherwise, it makes sense that the arguments array is empty. Also from what I recall using it, arguments[0] is the first argument you provide to the script. It is different from the command line arguments model to scripting languages or programming languages like C/C++ and Java. arguments[0] is not equal to the name of the script/application that is running unlike that model.

So basically, it works something like this:

js.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, ""); // where arg 0 = element (the web element), arg 1 = empty string ""

and if no argument is used, would be something like these:

js.executeScript("return window.location;");

js.executeScript("someJavascriptFunctionThatDoesNotTakeArguments();");

So for what you want to do, would be something more like this, outside of Selenium IDE:

js.executeScript("console.log(arguments[0]);", "foobar"); // this should work

js.executeScript("console.log(arguments);", "foo", "bar", "testing123"); //in theory, this may work, in reality, I don't know

If you wanted to check for the arguments, in javascript that you execute, you should do a comparison check whether arguments is null or the length of arguments received and log that out.

Reply all
Reply to author
Forward
0 new messages