简体   繁体   中英

Change the download path for Chrome/Chromium using Puppeteer

Iam using ubuntu 16.04 LTS and working on scraping project and trying to download a file using pupeteer But I am unable to change the download location in both chromium/chrome. I have already tried below option but it is not working:

await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: '/tmp'});   

Even when i enable this in headless:false mode then chromium crashes and in case of headless:true nothing gets downloaded anywhere not even in default location. Can some one please help me doing that. Please find below my code written in Express

const puppeteer = require('puppeteer')
var fs = require("fs")
async function downloadCSVFiles(){
console.log("Starting csv download process")
const browser = await puppeteer.launch({
    headless: true,
    defaultViewport: null,
    args: ['--no-sandbox']
})

const page = await browser.newPage()

const navigationPromise = page.waitForNavigation({waitUntil: 'networkidle0'})
await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: '/tmp'});    
const url = 'http://www.sitewithusercredential.com'
await page.goto(url)
await navigationPromise

console.log(page.url());

await page.waitForSelector('form > .row > .login-form > .sign-in-btn')
await page.click('form > .row > .login-form > .sign-in-btn')

await navigationPromise;
console.log(page.url())

 const options = await page.$$('.popOver > .customPopup > div > ul > li')
   
   await options[1].click(); //this will download the file

    await navigationPromise
}

At the top of your file, import path: const path = require('path')

Then for the downloadPath in the options you pass into client.send() , change it to path.resolve(__dirname, 'temp) :

await client.send("Page.setDownloadBehavior", {
      behavior: "allow",
      downloadPath: path.resolve(__dirname, 'tmp')
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM