Created
February 2, 2021 07:12
-
-
Save ankitsadariya/af0f7d1316dd60646feaae931ae5dfc4 to your computer and use it in GitHub Desktop.
read write and download file using puppeteer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer') | |
const fs = require('fs') | |
const path = require('path') | |
const { promisify } =require ('util') | |
const readFileAsync = promisify(fs.readFile) | |
const writeFileAsync = promisify(fs.writeFile); | |
(async () => { | |
const browser = await puppeteer.launch({}) | |
const page = await browser.newPage() | |
await page.setViewport({ width: 1200, height: 800 }) | |
await page.goto('http://www.softwaretestingtipsandtricks.com/') | |
const imageHref = await page.evaluate((sel) => { | |
return document.querySelector(sel).getAttribute('src').replace('/', '') | |
}, '.my-thumb') | |
const viewSource = await page.goto(imageHref); | |
const buffer = await viewSource.buffer(); | |
await writeFileAsync('accessibility.png', buffer); | |
console.log ('Image is saved'); | |
const content = await readFileAsync('accessibility.png'); | |
console.log('the file was read'); | |
await page.goto('http://www.softwaretestingtipsandtricks.com/2020/05/What-is-ada-accessibility-testing-how-to-do-it.html'); | |
await page.waitForSelector('.post-body'); | |
let element = await page.$('.post-body'); | |
let value = await page.evaluate(el => el.textContent, element) | |
fs.writeFileSync('file.txt', value); | |
const blogPost = await fs.readFileSync('file.txt'); | |
console.log('file was read'+blogPost); | |
browser.close() | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment