javascript - Puppeteer to save image open in the browser - Stack Overflow

I have a link for a (gif) image, obtained manually via 'open in new tab'. I want Puppeteer to

I have a link for a (gif) image, obtained manually via 'open in new tab'. I want Puppeteer to open the image and then save it to a file. If doing it in a normal browser I would click right button and choose 'save' from the context menu. Is there a simple way to perform this action in Puppeteer?

I have a link for a (gif) image, obtained manually via 'open in new tab'. I want Puppeteer to open the image and then save it to a file. If doing it in a normal browser I would click right button and choose 'save' from the context menu. Is there a simple way to perform this action in Puppeteer?

Share Improve this question asked Feb 13, 2022 at 17:52 Eugene BarskyEugene Barsky 6,0123 gold badges21 silver badges41 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

These lines of codes below will save Wikipedia image logo as filename logo.png

import * as fs from 'fs'
import puppeteer from 'puppeteer'
;(async () => {
    const wikipedia = 'https://www.wikipedia/'
    const browser = await puppeteer.launch()
    const page = (await browser.pages())[0]
    const get = await page.goto(wikipedia)
    const image = await page.waitForSelector('img[src][alt="Wikipedia"]')
    const imgURL = await image.evaluate(img => img.getAttribute('src'))
    const pageNew = await browser.newPage()
    const response = await pageNew.goto(wikipedia + imgURL, {timeout: 0, waitUntil: 'networkidle0'})
    const imageBuffer = await response.buffer()
    await fs.promises.writeFile('./logo.png', imageBuffer)
    await page.close()
    await pageNew.close()
    await browser.close()
})()

Please select this as the right answer if this help you.

In Puppeteer it's possible to right click, but it's not possible to automate the navigation through the "save as" menu. However, there is a solution outlined in the top answer here:

How can I download images on a page using puppeteer?

You can write the images to disk directly from the page response.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745101930a4611326.html

相关推荐

  • javascript - Puppeteer to save image open in the browser - Stack Overflow

    I have a link for a (gif) image, obtained manually via 'open in new tab'. I want Puppeteer to

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信