Skip to contents

[Experimental]

Execute a JavaScript function on zero or more arguments.

execute_js_expr() is a simpler version of execute_js_fn() that can evaluate simple expressions (e.g. "alert()"). To return a value, you must do so explicitly using "return".

These functions are experimental because their names and parameters are liable to change. Additionally, their behaviour can be inconsistent between different session types (chromote and selenium) and different browsers.

Usage

execute_js_fn(fn, ..., .timeout = NULL, .session = NULL, .debug = FALSE)

execute_js_expr(expr, ..., .timeout = NULL, .session = NULL, .debug = FALSE)

Arguments

fn

A string defining the function.

...

Arguments to the function/expression. These must be unnamed, since JavaScript does not support named arguments.

.timeout

How long to wait for any elements to exist in the DOM.

.session

The session to use, if ... does not contain any selenider elements.

.debug

Whether to print the final expression that is executed. Mostly used for debugging the functions themselves, but can also be used to identify problems in your own JavaScript code.

expr

An expression to execute.

Value

The return value of the JavaScript function, turned back into an R object.

Details

... can contain selenider_element/selenider_elements objects, which will be collected and then passed into the function. However, more complex objects (e.g. lists of selenider elements) will not be moved into the JavaScript world correctly.

Similarly, nodes and lists of nodes returned from a JavaScript function will be converted into their corresponding selenider_element/selenider_elements objects, while more complex objects will not. These elements are not lazy (see elem_cache()), so make sure you only use them while you are sure they are still on the page.

See also

Other global actions: back(), current_url(), get_page_source(), open_url(), reload(), take_screenshot()

Examples

html <- "
<button class='mybutton'>Click me</button>
"
session <- minimal_selenider_session(html)

execute_js_fn("(x, y) => x + y", 1, 1)

execute_js_expr("arguments[0] + arguments[1]", 1, 1)

execute_js_fn("x => x.click()", s(".mybutton"))

execute_js_expr("arguments[0].click()", s(".mybutton"))