Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active May 3, 2023 04:56
Show Gist options
  • Save miguelmota/9a21b72589769c1cb9380d7772e52065 to your computer and use it in GitHub Desktop.
Save miguelmota/9a21b72589769c1cb9380d7772e52065 to your computer and use it in GitHub Desktop.
Selenium Webdriver Node.js take screenshots of entire page
var webdriver = require('selenium-webdriver')
var assert = require('assert')
var fs = require('fs')
// Input capabilities
var capabilities = {
'browserName' : 'Chrome',
'browser_version' : '66.0',
'os' : 'Windows',
'os_version' : '10',
'resolution' : '1024x768',
'browserstack.user' : 'abc',
'browserstack.key' : '123'
}
var driver = new webdriver.Builder().
usingServer('http://hub-cloud.browserstack.com/wd/hub').
withCapabilities(capabilities).
build()
webdriver.WebDriver.prototype.saveScreenshot = filename => {
return driver.takeScreenshot().then(data => {
fs.writeFile(`${__dirname}/screenshots/${filename}`, data.replace(/^data:image\/png;base64,/,''), 'base64', err => {
if(err) throw err
})
})
}
async function main() {
var now = (Date.now()/1e3)|0
await driver.get('https://example.com')
var title = await driver.getTitle()
assert(title, 'Some title')
var totalHeight = await driver.executeScript('return document.body.offsetHeight')
var windowHeight = await driver.executeScript('return window.outerHeight')
for (var i = 0; i <= (totalHeight/windowHeight); i++) {
await driver.executeScript(`window.scrollTo(0, window.outerHeight*${i})`)
driver.saveScreenshot(`home-${i}-${now}.png`)
}
driver.quit()
console.log('done')
}
main()
@LucasOder66
Copy link

I think this code doesn't make one fullpage screenshot. It just makes several screenshots that of the whole page. Do you have a solution for just one picture of the whole page?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment