How to use Axios inside page.evaluate - Error: Evaluation failed: ReferenceError: Axios is not defined
See original GitHub issueHi Team,
I am trying to downalod file behind Site Logins with Axios, does not work. I am getting Error: Evaluation failed: ReferenceError: Axios is not defined.
const { chromium} = require("playwright");
const fs = require("fs");
const Axios = require("axios");
....
....
....
for (let val of listings) {
const downloadUrl = val.URL;
const name = val.Filename;
const downloadedContent = await page.evaluate(async (downloadUrl) => {
const response = await Axios({
url: downloadUrl,
method: "GET",
headers: {
"Upgrade-Insecure-Requests": "1",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/80.0.3987.163 Safari/537.36",
"Sec-Fetch-Dest": "document",
Accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
},
withCredentials: true,
});
return response.data;
}, downloadUrl);
fs.writeFile(`./${name}`, downloadedContent, () =>
console.log("Wrote file")
);
I have another function - fetch . This function works inside page.evaluate, can download and save file (also behind Site Logins because of { credentials: “include” } ) -> Problem is files are corrupt. I do not why. I am not able to open file. For CSV it works.
const { chromium} = require("playwright");
const fs = require("fs");
.....
.....
const downloadedContent = await page.evaluate(async (downloadUrl) => {
const fetchResp = await fetch(downloadUrl, { credentials: "include" });
return await fetchResp.text();
}, downloadUrl);
fs.writeFile(`./${name}`, downloadedContent, () =>
console.log("Wrote file")
I tryed also to call external function :
const { chromium, webkit } = require("playwright");
const fs = require("fs");
const Axios = require("axios");
const download_function = require("./download_function");
....
....
....
for (let val of listings) {
const downloadUrl = val.URL;
const name = val.Filename;
const downloadedContent = await page.evaluate(
async (Filename, downloadUrl) => {
const downloadfile = await download_function.downloadFileAxios(
name,
downloadUrl
);
},
name,
downloadUrl
);
and in external function i have:
const fs = require("fs");
const Path = require("path");
const Axios = require("axios");
async function downloadFileAxios(Filename, URL) {
const url = `${URL}`;
const path = Path.resolve(__dirname, "images", `${Filename}`);
const writer = fs.createWriteStream(path);
const response = await Axios({
url,
method: "GET",
headers: {
"Upgrade-Insecure-Requests": "1",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36",
"Sec-Fetch-Dest": "document",
Accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
},
responseType: "stream",
withCredentials: true,
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", resolve);
writer.on("error", reject);
});
}
exports.downloadFileAxios = downloadFileAxios;
how can i call axios inside page.evaluate with param: withCredentials: true and download files behind login page?
Thanks in advance for your help.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Error: Evaluation failed: ReferenceError: res is not defined
Official document - The method returns a Promise which resolves to the return value of pageFunction. This mean you can get back a...
Read more >Evaluation failed: ReferenceError: req is not defined". Im using ...
Im using puppeteer and im trying to get the clients input in the value input. Help! r/node - I get the error "Error: ......
Read more >Errors | Node.js v19.3.0 Documentation
Node.js generates system errors when exceptions occur within its runtime environment. These usually occur when an application violates an operating system ...
Read more >Developers - babel_runtime_core_js_get_iterator__ ... - Bountysource
Using this boilerplate in conjunction with puppeteer and trying to use it (page.evaluate and iterate over something), results in following error:
Read more >how to check if cookies is inside login response with axios
Use vue-cookies to have the client browser set a cookie once a login has been ... page.evaluate - Error: Evaluation failed: ReferenceError: Axios...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey, basically the evaluation functions work only with JavaScript which is directly inside of the passed function. (Under the hood it basically uses toString()).
So if you try to call external functions, imports/require statements, it does not work.
Regarding your axios question, here is some example maybe it helps: https://try.playwright.tech/?s=gvdfa
hi Team,
tnx a lot!