User Guide
Synapse Playwright adds new Storm APIs to allow you to interact with dynamic webpages within a Chromium instance.
Getting Started
Check with your Global Admin to enable permissions.
Additional deatils for the Page object API can be found here.
APIs return a tuple of ($ok, $data, $info)
where $ok
is a boolean flag
to indicate whether the call was successful. If not successful, $data
contains
an error string and the $info
dictionary may contain an excinfo
key
with additional exception information.
The page
Storm API accepts an optional conf
keyword argument, which if specified will apply
the dictionary as keyword arguments to the newly created context.
The browser:wait:timeout
key is used to specify a timeout when waiting for a free browser.
If not specified it defaults to None
.
For additional information on available arguments see the Playwright API new context documentation.
Examples
Load a page and save the rendered HTML to the Axon
$play = $lib.import(playwright.api)
($ok, $page, $info) = $play.page(loadurl="https://vertex.link")
if (not $ok) {
$lib.warn($page)
$lib.exit()
}
($ok, $data, $info) = $page.saveHtml()
if (not $ok) {
$lib.warn($data)
$lib.exit()
}
($size, $sha256) = $data
$lib.print("Saved HTML to Axon: size={size} sha256={sha256}", size=$size, sha256=$sha256)
Check the response code from loading a URL
$play = $lib.import(playwright.api)
($ok, $page, $info) = $play.page()
if (not $ok) {
$lib.warn($page)
$lib.exit()
}
($ok, $resp, $info) = $page.goto("https://vertex.link")
if (not $ok) {
$lib.warn($resp)
$lib.exit()
}
$code = $resp.status
if ($code != 200) {
$lib.warn("Page load received code: {code}", code=$code)
$lib.exit()
}
Execute JavaScript within the page
$play = $lib.import(playwright.api)
($ok, $page, $info) = $play.page(loadurl="https://vertex.link")
if (not $ok) {
$lib.warn($page)
$lib.exit()
}
($ok, $data, $info) = $page.jseval("document.querySelector('#foo').textContent")
if (not $ok) {
$lib.warn($data)
$lib.exit()
}
$lib.print("Element text: {text}", text=$data)
Fill an element with text and then click on an element
$play = $lib.import(playwright.api)
($ok, $page, $info) = $play.page(loadurl="https://vertex.link")
if (not $ok) {
$lib.warn($page)
$lib.exit()
}
// fill accepts a CSS selector and the text to fill
($ok, $data, $info) = $page.fill("#input", "myusername")
if (not $ok) {
$lib.warn($data)
$lib.exit()
}
// click accepts a CSS selector and auto-waits for navigation
($ok, $data, $info) = $page.click("#submit")
if (not $ok) {
$lib.warn($data)
$lib.exit()
}
Use a template to extract data as JSON
// Create a template that will return a list of href attributes from "a" elements
$template = ({
'type': 'list',
'select': 'a',
'template': {
'type': 'attr', 'name': 'href'
}
})
$play = $lib.import(playwright.api)
($ok, $page, $info) = $play.page(loadurl="https://vertex.link")
if (not $ok) {
$lib.warn($page)
$lib.exit()
}
($ok, $data, $info) = $play.htmlToJson($page, $template)
if (not $ok) {
$lib.warn($data)
$lib.exit()
}
for $link in $data {
$lib.print("Extracted link: {link}", link=$link)
}
Get and use the storage state
For additional details on the storage state format see the Playwright API storage state documentation.
Save the storage state to a user variable:
$play = $lib.import(playwright.api)
($ok, $page, $info) = $play.page(loadurl="https://vertex.link")
if (not $ok) {
$lib.warn($page)
$lib.exit()
}
($ok, $state, $info) = $page.getStorageState()
if (not $ok) {
$lib.warn($state)
$lib.exit()
}
$lib.user.vars.set(mystate, $state)
Specify the storage state for a new page:
$state = $lib.user.vars.get(mystate)
$conf = ({'storage_state': $state})
$play = $lib.import(playwright.api)
($ok, $page, $info) = $play.page(loadurl="https://vertex.link", conf=$conf)
if (not $ok) {
$lib.warn($page)
$lib.exit()
}
Closing a Page explicitly
A Page will continue to exist for the lifetime of a Storm query. The Page.close()
method
can be called to explicitly release the resources, and is most useful when multiple pages
will be opened within a given browser.
$play = $lib.import(playwright.api)
($ok, $page, $info) = $play.page(loadurl="https://vertex.link")
if (not $ok) {
$lib.warn($page)
$lib.exit()
}
// Attempts to use the page after calling close() will raise an exception
$page.close()