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.

# Browser based actions

Go back a tab.

window.history.back()

Tab back X amount of times.

window.history.go(-2)

# Clicks

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) {
  element.click()
  
	await delay(5000)

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

# Select Lists

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.keyboard.press('ArrowDown');
      await page.waitForTimeout(250);
  }
}

switch(marital) {
  case 'Married':
    break;
  case 'Single':
    arrowDownTimes(1);
    break;
  case 'Divorced':
    arrowDownTimes(2);
    break;
}

# Scraping

Get header and row values from an HTML table

const headers = Array.prototype.map.call(document.querySelectorAll('#org-insight__a11y-table tr'), function(tr){
  return Array.prototype.map.call(tr.querySelectorAll('th'), function(th){
    return th.textContent.trim()
    })
  })

const table = Array.prototype.map.call(document.querySelectorAll('#org-insight__a11y-table tr'), function(tr){
  return Array.prototype.map.call(tr.querySelectorAll('td'), function(td){
    return td.textContent.trim()
    })
  })

const data = headers.concat(table)

return data

# Date and time

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++) {
  	data[i].push([dt])
}

return data