Skip to main content

Run JavaScript in the app’s window. A script runs in the current frame (the frame entered, or the top page): its window and document. Paths follow http://127.0.0.1:4445/{token}; see the overview for the reply format and the errors.

Verified on macOS: sync and async, also in a frame

The script is the body of a function made with the current frame’s Function, called with that frame’s window as this and the arguments as arguments. It may run for as long as the session’s script timeout allows (30 seconds by default, no limit with null), then the command answers 500 script timeout. The server answers one request at a time, so a long script holds up the requests behind it.

Execute script#

POST /session/{sessionId}/execute/sync

The value the script returns is the result. A returned promise is awaited first.

sessionIdstringrequired#

The current session’s id.

scriptstringrequired#

The function body.

argsany[]#

The arguments. An element reference becomes the element, in arrays and objects too.

Default: "[]"

valueanyrequired#

The result, serialized as under Results.

Execute async script#

POST /session/{sessionId}/execute/async

The script gets one more argument, last: a callback. The value it is called with is the result.

sessionIdstringrequired#

The current session’s id.

scriptstringrequired#

The function body.

argsany[]#

The arguments, before the callback. An element reference becomes the element, in arrays and objects too.

Default: "[]"

valueanyrequired#

The value given to the callback, serialized as under Results.

Results#

A result goes back as JSON:

  • an element becomes its reference;
  • an array, a NodeList or an HTMLCollection becomes an array;
  • another object is copied key by key (its own enumerable keys);
  • undefined becomes null.

Errors#

StatuserrorWhen
400invalid argumentscript is missing or not a string (`script` is missing).
404no such element, stale element referenceAn element in args is unknown or gone.
404no such frameThe frame entered is gone or no longer same-origin.
500javascript errorThe script threw, or its promise was rejected; the message is the error’s.
500script timeoutThe script did not finish within the script timeout.