【发布时间】:2020-04-01 20:20:40
【问题描述】:
我有一个几乎完成的脚本,除了最后一部分。最后一部分需要我选择一个PDF文件上传。
我试过了:
inputUploadHandle = await page.$('input[type=file]');
await inputUploadHandle.uploadFile('/myfile.pdf');
我得到一个错误:
(node:20704) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'uploadFile' of undefined
然后我选择我的文件:
它显示它是这样上传的:
但是当我也尝试使用时:
const[fileChooser] = await Promise.all([
page.waitForFileChooser(),
page.click('#filingDocumentSection > div:nth-child(2) > div > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div > ul > li > div > div.col-sm-7 > button > i'),
])
await fileChooser.accept(['/myfile.pdf']);
现在我确实看到发生了一些事情,但我认为网站实际上并未接受 pdf 文件。控制台读取没有错误,页面看起来像是在等待我做其他事情。我注意到的是,当我这样做时,我无法再单击您将上传文件的框。它就像那个按钮失去了它的功能。
看起来对话框的按钮已打开,然后立即关闭,但我没有看到该文件实际上已被接受或上传。
我用它来查看我是否在实际页面控制台中使用了正确的查询选择器:
document.querySelectorAll('input[type=file]')[0];
返回:
<input type="file" ngf-select="" ngf-drop="" ngf-drag-over-class=
"{accept: 'dragover', reject: 'dragover-err', delay: 50}" ngf-multiple="{{filingComponent.AllowMultipleFiles}}"
ng-attr-accept="{{filingComponent.AllowedFileTypes}}"
ngf-accept="filingComponent.AllowedFileTypes"
ngf-change="UploadFiles(filingComponent, $files)"
title="File system upload" accept="application/pdf" multiple="multiple">
现在在控制台的后端,我确实看到当我手动上传文件时调用了函数:
我查看了执行 .UpLoadFiles() 的函数:
n.DownloadDocument = function (t, i, r, u) {
var f = !1;
u !== undefined & u !== null && u === 1 && (f = !0);
!f && n.SelectedItem.Filing.ViewStampedDocumentsUrl ? window.open(n.SelectedItem.Filing.ViewStampedDocumentsUrl) : $.get(t.format(i, r)).success(function (n) { window.location = n.DownloadUrl }) };
n.UploadFiles = function (t, i) {
var r, u;
n.FileToUploadLength = 0;
r = i.length;
console.group("$scope.UploadFiles()");
angular.isArray(i) && i.length > 0 && (n.SelectedItem.Filing.Id === 0 ? (
u = i.shift(),
n.UploadFileChain(u, t, r, i)) : _.each(i, function (i) {
n.UploadFileChain(i, t, r, []) }));
console.groupEnd() };
不知道有没有帮助....
除了 .uploadfile 或 .accept 之外,puppeteer 是否使用了一些东西来使对话框选择您的文件?
【问题讨论】:
标签: javascript node.js submit puppeteer