# Selenium

const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const {By, until} = require('selenium-webdriver');
const options = new chrome.Options();
options.addArguments('--headless');
const driver = new webdriver.Builder()
        .forBrowser('chrome')
        .setChromeOptions(options)
        .build();
(async () => {
    await driver.get(urlWithToken);
})

# 网页截图

node.js + selenium

const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const {By, until} = require('selenium-webdriver');
const options = new chrome.Options();
options.addArguments('--headless');
const driver = new webdriver.Builder()
        .forBrowser('chrome')
        .setChromeOptions(options)
        .build();

const url = 'http://www.example.com'
const tokenValue = 'WY1ksZ9wUbyTua5Cij2GJDIPMXRKLq6N'
const urlWithToken = url + '?user-token=' + encodeURICompoent(tokenValue);

async function captureScreenshot() {
    try {
        // await driver.get(url);
        // await driver.manage().addCookie({name:'user-token', value: tokenValue})
        // await driver.get(url);
        await driver.get(urlWithToken);
        // 等待页面加载完成
        await driver.wait(until.elementLocated(By.css('#page-anchor .is-loaded')), 60 * 1000);
        // 调整窗口高度,完成长截图
        const anchorEl = driver.findElement(By.css('#page-anchor'))
        const anchorRect = anchorEl.getRect()
        const pageHeight = anchorRect.y || 4096;
        await driver.manage().window().setSize({width: 1920, height: pageHeight})
        // 截图
        const data = await driver.takeScreenshot();
        // 写入文件或上传
        require('fs').writeFileSync('example.png', data, 'base64');
        await driver.quit();
    } finally {
        await driver.quit();
    }
}
captureScreenshot();