Context:
Code Snippet
const { chromium, webkit, firefox } = require('playwright');
(async () => {
const browser = await chromium.launch({ headless: false });
const context = await browser.newContext({ bypassCSP: true });
const page = await context.newPage();
await page.goto('https://www.behance.net', { waitUntil: 'networkidle0' });
const myText = await page.evaluate(async () => {
const response = await fetch('https://example.com');
const text = await response.text();
return text;
});
console.log(myText);
await browser.close();
})();
Describe the bug
Hi team. I'm seeing this error when I try to run the above code.
UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: Failed to fetch
The request I'm making is cross-origin, so this could be a CSP or CORS error, but the error message doesn't give me any information about what the error is. It's possible this is out of scope for what Playwright is doing, but I thought I'd at least check. Thank you!
Sadly, Failed to fetch is the only information available in the exception itself. The rest is available in Chrome via the console. You can too log what is getting into the console:
page.on('console', m => console.log(m.text()));
That way you get this:
Access to fetch at 'https://example.com/' from origin 'https://www.behance.net' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Failed to load resource: net::ERR_FAILED
I wonder if we should provide an option that translates console messages / errors into the node console with less effort.
Thank you for the explanation @pavelfeldman. That does sound like it could be a nice feature. I'll go ahead and close this since it seems like a separate thing.
Most helpful comment
Sadly,
Failed to fetchis the only information available in the exception itself. The rest is available in Chrome via the console. You can too log what is getting into the console:page.on('console', m => console.log(m.text()));That way you get this:
I wonder if we should provide an option that translates console messages / errors into the node console with less effort.