Reputation: 451
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
}
Upvotes: 2
Views: 5594
Reputation: 1638
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')
})
Upvotes: 4