Skip to content

Instantly share code, notes, and snippets.

@LearnWebCode
Created July 21, 2021 23:41
Show Gist options
  • Select an option

  • Save LearnWebCode/31f8a20ef7e4324aca322a21dbfc3d7e to your computer and use it in GitHub Desktop.

Select an option

Save LearnWebCode/31f8a20ef7e4324aca322a21dbfc3d7e to your computer and use it in GitHub Desktop.

Revisions

  1. LearnWebCode revised this gist Jul 21, 2021. No changes.
  2. LearnWebCode created this gist Jul 21, 2021.
    37 changes: 37 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    // in a new folder be sure to run "npm init -y" and "npm install puppeteer"
    const puppeteer = require("puppeteer")
    const fs = require("fs/promises")

    async function start() {
    const browser = await puppeteer.launch()
    const page = await browser.newPage()
    await page.goto("https://learnwebcode.github.io/practice-requests/")

    const names = await page.evaluate(() => {
    return Array.from(document.querySelectorAll(".info strong")).map(x => x.textContent)
    })
    await fs.writeFile("names.txt", names.join("\r\n"))

    await page.click("#clickme")
    const clickedData = await page.$eval("#data", el => el.textContent)
    console.log(clickedData)

    const photos = await page.$$eval("img", imgs => {
    return imgs.map(x => x.src)
    })

    await page.type("#ourfield", "blue")
    await Promise.all([page.click("#ourform button"), page.waitForNavigation()])
    const info = await page.$eval("#message", el => el.textContent)

    console.log(info)

    for (const photo of photos) {
    const imagepage = await page.goto(photo)
    await fs.writeFile(photo.split("/").pop(), await imagepage.buffer())
    }

    await browser.close()
    }

    start()