Nodejs后端自动化测试

偶然看到收藏一下

const puppeteer = require('puppeteer');
const fs = require('fs');

(async () => {
    const browser = await puppeteer.launch({
        headless: true
    })
    
    const page = await browser.newPage();
    
    await page.goto('https://juejin.im');

    await page.waitFor(1000);

    await page.evaluate(async () => {
        window.scrollTo(0,document.querySelector('.entry-list').scrollHeight);
    });
    await page.waitFor(500);
    await page.evaluate(async () => {
        window.scrollTo(0,document.querySelector('.entry-list').scrollHeight);
    });
    await page.waitFor(500);
    const res = await page.evaluate(async () => {
        window.scrollTo(0,document.querySelector('.entry-list').scrollHeight)
        window.scrollTo(0,document.querySelector('.entry-list').scrollHeight)
        return [].map.apply(document.querySelectorAll('.info-row.title-row .title'),[v => ({
            title:v.innerText,
            link:v.href,
        })])
    });
    fs.writeFileSync('juejin.txt',JSON.stringify(res));
    browser.close();
})()

 

上一篇:js原生实现三级联动下拉菜单


下一篇:DOM操作元素