Drive a headless phantom.js browser via the WebDriver protocol. It needs phantom.js running in WebDriver mode.

Usage

s <- Session$new(host = "127.0.0.1", port = 8910)

s$delete()
s$status()

s$go(url)
s$getUrl()
s$goBack()
s$goForward()
s$refresh()
s$getTitle()
s$getSource()
s$takeScreenshot(file = NULL)

s$findElement(css = NULL, linkText = NULL,
    partialLinkText = NULL, xpath = NULL)
s$findElements(css = NULL, linkText = NULL,
    partialLinkText = NULL, xpath = NULL)

s$executeScript(script, ...)
s$executeScriptAsync(script, ...)

s$setTimeout(script = NULL, pageLoad = NULL, implicit = NULL)

s$moveMouseTo(xoffset = 0, yoffset = 0)
s$click(button = c("left", "middle", "right"))
s$doubleClick(button = c("left", "middle", "right"))
s$mouseButtonDown(button = c("left", "middle", "right"))
s$mouseButtonUp(button = c("left", "middle", "right"))

s$readLog(type = c("browser", "har"))
s$getLogTypes()

s$waitFor(expr, checkInterval = 100, timeout = 3000)

Arguments

s

A Session object.

host

Host name of phantom.js.

port

Port of phantom.js.

url

URL to nagivate to.

file

File name to save the screenshot to. If NULL, then it will be shown on the R graphics device.

css

Css selector to find an HTML element.

linkText

Find HTML elements based on their innerText.

partialLinkText

Find HTML elements based on their innerText. It uses partial matching.

xpath

Find HTML elements using XPath expressions.

script

For executeScript and executeScriptAsync. JavaScript code to execute. It will be placed in the body of a function.

...

Arguments to the script, they will be put in a list called arguments. Element objects are automatically transformed to DOM element in JavaScript.

script

For setTimeout. Script execution timeout, in milliseconds. More below.

pageLoad

Page load timeout, in milliseconds. More below.

implicit

Implicit wait before calls that find elements, in milliseconds. More below.

xoffset

Horizontal offset for mouse movement, relative to the current position.

yoffset

Vertical offset for mouse movement, relative to the current position.

button

Mouse button. Either one of "left", "middle", "right", or an integer between 1 and 3.

type

Log type, a character scalar.

expr

A string scalar containing JavaScript code that evaluates to the condition to wait for.

checkInterval

How often to check for the condition, in milliseconds.

timeout

Timeout for the condition, in milliseconds.

Details

Session$new() creates a new WebDriver session.

s$delete() deletes a WebDriver session.

s$status() returns a status message from the server. It is a named list, and contains version numbers and capabilities.

s$go() navigates to the supplied URL.

s$getUrl() returns the current URL.

s$goBack() is like the web browser's back button. It goes back to the previous page.

s$goForward() is like the web browser's forward button.

s$refresh() is like the web browser's refresh button.

s$getTitle() returns the title of the current page.

s$getSource() returns the complete HTML source of a page, in a character scalar.

s$takeScreenshot() takes a screenshot of the current page. You can save it to a PNG file with the file argument, or show it on the graphics device (if file is NULL).

s$findElement() finds a HTML element using a CSS selector, XPath expression, or the innerHTML of the element. If multiple elements match, then the first one is returned. The return value is an Element object.

s$findElements() finds HTML elements using a CSS selector, XPath expression, or the innerHTML of the element. All matching elements are returned in a list of Element objects.

s$executeScript() executes JavaScript code. It places the code in the body of a function, and then calls the function with the additional arguments. These can be accessed from the function via the arguments array. Returned DOM elements are automatically converted to Element objects, even if they are inside a list (or list of list, etc.).

s$executeScriptAsync() is similar, for asynchronous execution. It place the script in a body of a function, and then calls the function with the additional arguments and a callback function as the last argument. The script must call this callback function when it finishes its work. The first argument passed to the callback function is returned. Returned DOM elements are automatically converted to Element objects, even if they are inside a list (or list of list, etc.).

s$setTimeout() sets various timeouts. The ‘script’ timeout specifies a time to wait for scripts to run. The sQuotepage load timeout specifies a time to wait for the page loading to complete. The ‘implicit’ specifies a time to wait for the implicit element location strategy when locating elements. Their defaults are different in the standard and in Phantom.js. In Phantom.js the ‘script’ and ‘page load’ timeouts are set to infinity, and the ‘implicit’ waiting time is 200ms.

s$moveMouseTo() moves the mouse cursor by the specified offsets.

s$click() clicks the mouse at its current position, using the specified button.

s$doubleClick() emulates a double click with the specified mouse button.

s$button_down() emulates pressing the specified mouse button down (and keeping it down).

s$button_up() emulates releasing the specified mouse button.

s$getLogTypes() returns the log types supported by the server, in a character vector.

s$readLog() returns the log messages since the last readLog call, in a data frame with columns timestamp, level and message.

s$waitFor() waits until a JavaScript expression evaluates to true, or a timeout happens. It returns TRUE is the expression evaluated to true, possible after some waiting. If the expression has a syntax error or a runtime error happens, it returns NA.

See also