Javascript snippets

Handy JS snippets that can be cut and pasted into a 'Write javascript' Step to perform a function like fetching yesterday's date.

# Go back in a tab with javascript

Go back a tab.


Tab back X amount of times.


# Click on something with javascript

Find a button by text, click and pause 5 seconds. Repeat until button is no longer present on the page.

const delay = ms => new Promise(res => setTimeout(res, ms))

let element = document.evaluate("//button[contains(., 'Load more')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE).singleNodeValue

while (element) {
	await delay(5000)

  element = document.evaluate("//button[contains(., 'Load more')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE).singleNodeValue

# Interact with a select list with javascript

Use keyboard commands (arrow down) for difficult select-lists

// First, either 1) Click to open the difficult select-list, 
// OR 2) Use the tab-key to select the element
let marital = 'Single' 

async function arrowDownTimes(times) {
  for(var i = 0; i < times; i++){
      await page.waitForTimeout(250);

switch(marital) {
  case 'Married':
  case 'Single':
  case 'Divorced':

# Scrape with javascript

Get header and row values from an HTML table

const headers ='#org-insight__a11y-table tr'), function(tr){
  return'th'), function(th){
    return th.textContent.trim()

const table ='#org-insight__a11y-table tr'), function(tr){
  return'td'), function(td){
    return td.textContent.trim()

const data = headers.concat(table)

return data

Scrape element attribute

let results = []
let els = document.querySelectorAll("div:nth-child(1) > input")

for (const el of els) {

return results

Scrape body and return result

return [[document.querySelector("body").innerHTML]]

# Get the date and time with javascript

Fetch yesterday's date.

const yesterday = new Date()
yesterday.setDate(yesterday.getDate() - 1)
return [[yesterday.toLocaleDateString('en-US')]]

Get the date X days ahead (Edit '14' to change the number of days).

const targetDate = new Date()
targetDate.setDate(targetDate.getDate() + 14)
return [[targetDate.toLocaleDateString('en-US')]]

Append timestamp to scraped data

let data = [all-interaction-data] // Token from previous scrape step
let dt = new Date().toLocaleString("en-GB", { timeZone: 'Europe/London' })

for (var i = 0; i < data.length; i++) {

return data