Playwright: [BUG] setInputFiles does fetch that fails due to CSP

Created on 24 Jan 2020  Â·  8Comments  Â·  Source: microsoft/playwright

Hi, I am trying to upload a file but getting below error:
Error: Evaluation failed: TypeError: Failed to fetch
at __playwright_evaluation_script__:3:30
at Array.map ()
at __playwright_evaluation_script__:2:46
at CRExecutionContext.evaluate

Code:
const drop= await page.waitForSelector("//input[@type='file']");
await drop.setInputFiles('pic.png')

Also, the same was working with below Puppeteer code:
const drop= await page.waitForXPath("//slot/input[@type='file']");
await drop.uploadFile('pic.png');

browser-chromium

Most helpful comment

This error occurs when content security policy on document is enabled. Fix it by disabling CSP by setting bypassCSP to true in newContext method.

All 8 comments

Can you provide a small script that reproduces the problem? I tried and was unable to:

const {chromium} = require('playwright');
chromium.launch().then(async browser => {
    const [page] = await browser.defaultContext().pages();
    await page.setContent(`<input type="file">`);
    const input = await page.$('input');
    await input.setInputFiles('index.js');
    await browser.close();
});

I have tried the code and it works fine if the button is clickable but in my scenario the button is kind overlays by a label. If I do drop.click(); it fails so I tried drop.setInputFiles('pic.png') but this doesnt seem to work for me. Is it an open issue?

Clickability should not affect setInputFiles, so the reason should be something else. If you could share the page where this does not work (a link or maybe saved as mhtml), I would be happy to take a look. Thank you!

Closing because we cannot repro this for a while. Feel free to reopen if this is still an issue.

This error occurs when content security policy on document is enabled. Fix it by disabling CSP by setting bypassCSP to true in newContext method.

Disabling CSP fixed it for me as well, thanks for the hint.

(There should be a user-editable (possibly via PRs) FAQ section on playwright.dev where people can put their HOWTOs, tips & tricks and so on.)

@JoelEinbinder @dgozman Is disabling CSP the only/right way to make file upload work in this case? Because disabling it may have side effects (e.g. on my page, after disabling CSP I get a cookie consent modal displayed). If disabling CSP should not be the right way and if you're still interested in a repro case, I could provide on. In my case, if CSP is not disabled, I get the following error message when setInputFiles ( await page.setInputFiles('#income-upload', 'uploads/pirate.jpg'); ) is executed:

pw:api => page.focus started +1ms
pw:api waiting for selector "text=Ziehen Sie die hochzuladenden Dateien in diesen Bereich" [] +0ms
pw:api selector resolved to visible Ziehen Sie die hochzuladenden Dateien in diesen B… [] +4ms
pw:api <= page.focus succeeded +5ms
pw:api => page.setInputFiles started +0ms
pw:api waiting for selector "#income-upload" [] +0ms
pw:api selector resolved to hidden

[api] selector resolved to hidden
ghost picture ghost on 2 Sep 2020

(There should be a user-editable (possibly via PRs) FAQ section on playwright.dev where people can put their HOWTOs, tips & tricks and so on.)

You can send a PR editing documentation source, it will be reflected on playwright.dev.

As for the fetch error, let me take a look. Disabling CSP is a big hammer, so I'd prefer another solution.

@dgozman Thanks for pointing out the link to the documentation source.

Also thanks for looking again into this error. I have sent you the repro URL via email.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

osmenia picture osmenia  Â·  4Comments

grajesh690 picture grajesh690  Â·  3Comments

arjunattam picture arjunattam  Â·  4Comments

juliomatcom picture juliomatcom  Â·  3Comments

andyricchuiti picture andyricchuiti  Â·  4Comments