【发布时间】:2020-07-03 10:50:12
【问题描述】:
我已经关注了有关如何使用 Puppeteer 的 uploadFile() 方法的文档和示例。
但是,我不断收到此错误消息Evaluation failed: TypeError: Failed to fetch
我不知道为什么我不能自动上传。我有一个保存在我的仓库中的文件,我正在尝试上传。
这是uploadFile() 代码的sn-p。
await page.waitForSelector('input[type="file"]');
const fileInput = await page.$('input[type="file"]');
const filePath = 'path-to-my-file';
await fileInput.uploadFile(filePath);
关于我应该看哪里的任何指针?
* “解决方案” * 看起来 Puppeteer 版本 2.1.1 可能存在此 GitHub 票证指示的问题。 https://github.com/puppeteer/puppeteer/issues/5503 如果您检查 repo(截至 2020 年 3 月 23 日),还有其他几个与同一问题相关的未决票证。
在该票证中,用户降级到 2.0.0 并且附件功能正常工作。
【问题讨论】:
-
确保正确解析路径。在脚本顶部添加
const Path = require('path');,然后在脚本中添加const filePath = Path.join(__dirname, 'path-to-file-relative-to-this-script.jpg');。这个对我有用。如果您使用的是 Unix,请确保该文件具有访问权限 -
@blex 刚刚尝试过,但我仍然收到
Evaluation failed: TypeError: Failed to fetch错误。
标签: javascript google-chrome testing jestjs puppeteer