千巷貓影
2022-06-09 19:16:01
我有一個幾乎完成的腳本,除了最后一部分。最后一部分需要我選擇一個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">現在在控制臺的后端,我確實看到手動上傳文件時調用了函數:puppeteer 是否使用 .uploadfile 或 .accept 讓對話框選擇您的文件?
2 回答

偶然的你
TA貢獻1841條經驗 獲得超3個贊
所以根據這個: https ://github.com/puppeteer/puppeteer/issues/5503
您必須降級 Puppeteer。
我所做的只是:npm i [email protected]
瞧……我的代碼有效。
答案是:
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']);
但是你必須使用 puppeteer 2.0.0

慕森王
TA貢獻1777條經驗 獲得超3個贊
我很久以前在其他公司嘗試過這樣做,我不認為你在做什么是錯誤的,但我認為它可能是你正在使用的 puppeteer 版本。我忘記了哪個版本可以工作,但這里的 const[fileChooser] =代碼肯定可以工作。
添加回答
舉報
0/150
提交
取消