I am using puppeteer to login into my account of a content provider I work with and download a lot of zips. I can easily get the array with all the links I need to download then I loop through the array and use browser.downloads.download (code taken from MDN) to download each link. The problem is that even before it starts to download it tells me:
JavaScript
x
2
1
error: TypeError: Cannot read property 'download' of undefined
2
Here is my code:
JavaScript
1
62
62
1
const puppeteer = require('puppeteer');
2
require('dotenv').config();
3
4
(async () => {
5
const browser = await puppeteer.launch({
6
headless: false,
7
args: ['--start-maximized'],
8
defaultViewport: null,
9
});
10
11
const page = await browser.newPage();
12
13
try {
14
await page.goto(process.env.GROB_URL);
15
page.setDefaultNavigationTimeout(0);
16
17
await page.waitForSelector('input[name=user]', {
18
visible: true,
19
});
20
//login
21
await page.type('input[name=user]', process.env.GROB_USER);
22
await page.type('input[name=pass]', process.env.GROB_PASS);
23
await page.click('input#head-login');
24
25
await page.waitForSelector('.light-highlight-row');
26
27
const setValue = new Set();
28
// get the nodelist array with all the download links
29
let arr = await page.$$('.light-highlight-row td:nth-child(8) a');
30
31
for (let item of arr) {
32
const jsonValue = await item.getProperty('href');
33
const value = await jsonValue.jsonValue();
34
setValue.add(value);
35
}
36
37
const finalArray = Array.from(setValue);
38
//all good till here
39
40
function onStartedDownload(id) {
41
console.log(`Started downloading: ${id}`);
42
}
43
44
function onFailed(error) {
45
console.log(`Download failed: ${error}`);
46
}
47
48
//donwload all of them
49
finalArray.forEach((link) => {
50
var downloadUrl = link;
51
var downloading = browser.downloads.download({
52
url: downloadUrl,
53
});
54
downloading.then(onStartedDownload(link), onFailed(link));
55
});
56
page.waitForTimeout(500_000);
57
58
} catch (error) {
59
console.log('error: ', error);
60
}
61
})();
62
It somehow tells me the browser is undefined, but it is JS API.
Advertisement
Answer
There is no this method or property downloads in Browse object, you can check it here on puppeteer docs.
you can try using a http request to get your files, for example using this strategy below, I couldn’t try it, but I hope it is usefull for you =)
JavaScript
1
71
71
1
const puppeteer = require('puppeteer');
2
require('dotenv').config();
3
4
async function downloadFile(downloadUrl) {
5
6
await fetch(downloadUrl)
7
.then(res => res.blob())
8
.then(blob => {
9
var file = window.URL.createObjectURL(blob);
10
window.location.assign(file);
11
});
12
13
}
14
15
16
(async () => {
17
const browser = await puppeteer.launch({
18
headless: false,
19
args: ['--start-maximized'],
20
defaultViewport: null,
21
});
22
23
const page = await browser.newPage();
24
25
try {
26
await page.goto(process.env.GROB_URL);
27
page.setDefaultNavigationTimeout(0);
28
29
await page.waitForSelector('input[name=user]', {
30
visible: true,
31
});
32
//login
33
await page.type('input[name=user]', process.env.GROB_USER);
34
await page.type('input[name=pass]', process.env.GROB_PASS);
35
await page.click('input#head-login');
36
37
await page.waitForSelector('.light-highlight-row');
38
39
const setValue = new Set();
40
// get the nodelist array with all the download links
41
let arr = await page.$$('.light-highlight-row td:nth-child(8) a');
42
43
for (let item of arr) {
44
const jsonValue = await item.getProperty('href');
45
const value = await jsonValue.jsonValue();
46
setValue.add(value);
47
}
48
49
const finalArray = Array.from(setValue);
50
//all good till here
51
52
function onStartedDownload(id) {
53
console.log(`Started downloading: ${id}`);
54
}
55
56
function onFailed(error) {
57
console.log(`Download failed: ${error}`);
58
}
59
60
//donwload all of them
61
finalArray.forEach((link) => {
62
//here you can call the download funcion passing link and a filename
63
await downloadFile(link)
64
});
65
page.waitForTimeout(500_000);
66
67
} catch (error) {
68
console.log('error: ', error);
69
}
70
})()
71