Puppeteer how to save/set download path when download starts from different tab not the current one
See original GitHub issueHello In my code i am trying to download file. When puppeteer clicks on a tag it triggers download but also opens new tab for 2-3sec where this download is held (i think). SO i thinkl i need to set download behaviour to that tab but problme is it flashes and browers.pages() also did not catch it. SO how can i set different folder for this download? Tell us about your environment:
- Puppeteer version: 1,17.0
- Platform / OS version: Windows 10
- URLs (if applicable): no
- Node.js version: Fresh instalation
What steps will reproduce the problem?
const client = await page.target().createCDPSession(); await client.send("Page.setDownloadBehavior", { behavior: "allow", downloadPath: path.resolve(__dirname, "reports/asd") });
Please include code that reproduces the issue.
async function main() {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.setViewport({ width: 1366, height: 800 });
await page.goto(endpoint, { waitUntil: "networkidle2", timeout: 0 });
await page.waitFor(3000);
await page.type("#username", login);
await page.type("#password", pass);
// click and wait for navigation
await page.click("#login"),
await page.waitForNavigation({ waitUntil: "networkidle2" });
await page.waitFor(2000);
const client = await page.target().createCDPSession();
await client.send("Page.setDownloadBehavior", {
behavior: "allow",
downloadPath: path.resolve(__dirname, "reports/asd")
});
await page.waitForXPath('//*[@id="ctl00_ctl00_masterMain_cphMain_repViewable_ctl01_thisItemLink"]');
const [setting] = await page.$x('//*[@id="ctl00_ctl00_masterMain_cphMain_repViewable_ctl01_thisItemLink"]');
if (setting) setting.click();
await page.waitFor(3000);
let pages = await browser.pages();
await page.waitFor(2000);
await pages[2].waitForXPath('/html/body/table/tbody/tr[17]/td[6]/a');
const [newVID] = await pages[2].$x('/html/body/table/tbody/tr[17]/td[6]/a');
if (newVID) newVID.click();
await pages[2].waitFor(3000)
await pages[2].close()
await pages[2].waitFor(5000)
await pages[2].waitForXPath('//*[@id="freeze"]/table/tbody/tr[2]/td/table/tbody/tr[3]/td/a');
const [neww] = await pages[2].$x('//*[@id="freeze"]/table/tbody/tr[2]/td/table/tbody/tr[3]/td/a');
if (neww) neww.click();
await pages[2].waitFor(4000)
await pages[2].waitFor(4000)
await pages[2].waitForXPath('/html/body/div/table/tbody/tr[3]/td/a');
const [fin] = await pages[2].$x('/html/body/div/table/tbody/tr[3]/td/a');
if (fin) fin.click();
}
main()
What is the expected result?
to download file to folder set in code not to default downloads
What happens instead?
file is save to downloads
Issue Analytics
- State:
- Created 4 years ago
- Comments:5
Top GitHub Comments
I wasn’t able to figure out a way to set a default downloads folder (why this isn’t a setting on the browser object is beyond me). So what I did was use page.exposeFunction() to override the window.open function on the page that will try to open the new tab. Then in the callback, I used page.goto() to go to the download URL within the same tab instead of opening a new one. It had to be enclosed in a try/catch because it throws ERR_ABORTED when the file being browsed to is a download.
Using Puppeteer Extra you can configure the folder where you want to download:
`const puppeteer = require(“puppeteer-extra”);
var dir = process.cwd () puppeteer.use(require(‘puppeteer-extra-plugin-user-preferences’) ( {userPrefs: { download: { prompt_for_download: false, open_pdf_in_system_reader: true, default_directory:dir, }, plugins: { always_open_pdf_externally: true }, }}));`