在非输入type =“ file”上载文件

时间:2018-07-24 06:33:53

标签: javascript node.js puppeteer

我正在尝试使用Puppeteer上传文件,但是没有<input type="file">,因此,以下代码失败:-

let input = await page.$("input[type=file]");
await input.uploadFile("./test.pdf");

还有其他一些输入字段,但是在它们上进行上载也会失败,并显示以下错误:-

  

错误:协议错误(DOM.setFileInputFiles):节点不是文件   输入元素

以下是上传按钮的代码:-

<form name="commonform" id="form_bookinteriorPrimaryForm" method="post" action="https://tsw.createspace.com/title/ajax/paperback/8871655/setup/book_interior/processservice?serviceId=88073343&amp;serviceKey=bookinterior&amp;taskKey=book_interior&amp;submittedBy=4241370&amp;pageCount=120">
<div class="task" id="task123780522">
<div class="action FILE_UPLOAD" id="t123780522a_interiorfile">
<input type="hidden" id="form_bookinterior_upload_tsw_setup_book_interior_tsw_book_interior__upload_s3name" name="TSW_BOOK_INTERIOR-">
<input type="hidden" id="form_bookinterior_upload_tsw_setup_book_interior_tsw_book_interior__upload_asyncTaskPostfix" name="t123780522a_interiorfile_asyncTaskPostfix">
<span class="formFieldContainer" id="form_bookinterior_upload_tsw_setup_book_interior_tsw_book_interior__uploadComplete" style="display: none;">
       <div class="contentFormLabel">
          <span>Interior File</span>
       </div>
   <div class="contentFormField">
<span id="form_bookinterior_upload_tsw_setup_book_interior_tsw_book_interior__uploadUserFileName" class="uploadFileName">interior - Copy.pdf</span><br>
        <a href="https://tsw.createspace.com/title/8871655/setup/book_interior#" target="" class="changeUploadLink" onclick="universalAjaxHandler.handleOnClick([['element', 'form_bookinterior_upload_tsw_setup_book_interior_tsw_book_interior__uploadComplete', 'hide'], ['element', 'form_bookinterior_upload_tsw_setup_book_interior_tsw_book_interior__uploadEdit', 'show']]); return false;"><span class="">Upload a different file</span></a>
<div class="clearer"></div>
      <br>
   </div>
</span>
<span class="fixedFormUploadEdit" id="form_bookinterior_upload_tsw_setup_book_interior_tsw_book_interior__uploadEdit" style="">
<div id="t123780522a_interiorfile_upload_field_containerTop" class="cspContainerTop" style="display: none;"></div>
<div id="t123780522a_interiorfile_upload_field_container" class="cspContainer">
<div id="tsw_setup_book_interior_tsw_book_interior__upload_field_container" class="cspContainer formFieldContainer">
    <div class="contentFormLabel">
<span class="">Interior File</span>
       <span class="requiredColor">*</span>
    </div>
<div class="contentFormField">
<span id="tsw_setup_book_interior_tsw_book_interior__uploadIFrame" style="display: none;">
</span>
<span id="tsw_setup_book_interior_tsw_book_interior__uploadSWF" style="">
   <div style="float: left;">
      <input type="text" class="contentTextField formItemNormal" id="tsw_setup_book_interior_tsw_book_interior__uploadTextTarget" style="width: 200px; margin-top: 5px;" disabled="true">
   </div>
   <div style="float: left;">
      <div class="round_button_link" id="tsw_setup_book_interior_tsw_book_interior__uploadUploadContainer">
         <div class="round_button_link_left"></div>
         <div class="round_button_link_middle">
            <div style="margin-top: 4px;">Browse</div>
         </div>
         <div class="round_button_link_right"></div>
      </div>
    </div>
</span>
<div class="clearer"></div>
<span class="note">The following formats are accepted: pdf,doc,docx,rtf</span>
<script type="text/javascript">
/* <![CDATA[ */
if (window.CSP_OK) {
      document.observe('dom:loaded', function(e) {
         blockHandler = false;
   new FormInputField('tsw_setup_book_interior_tsw_book_interior__uploadTextTarget');
      });
}
/* ]]> */
</script>
   <div id="tsw_setup_book_interior_tsw_book_interior__upload_results" class="cspError contentFormError" style="display: none;"></div>
</div>
<div style="clear: left;"></div>
</div>
<script type="text/javascript">
/* <![CDATA[ */
if (window.CSP_OK) {
      document.observe('dom:loaded', function(e) {
         blockHandler = false;
   new FormField('tsw_setup_book_interior_tsw_book_interior__upload_field_container');
      });
}
/* ]]> */
</script>
   <div class="clearer"></div>
    <div id="t123780522a_interiorfile_upload_results" class="cspError formFieldDefaultError" style="display: none;"></div>
</div>
<div class="clearer"></div>
<script type="text/javascript">
/* <![CDATA[ */
if (window.CSP_OK) {
      document.observe('dom:loaded', function(e) {
         blockHandler = false;
   new FormField('t123780522a_interiorfile_upload_field_container');
      });
}
/* ]]> */
</script>
   <br>
</span>
<div class="cspError formFieldDefaultError" style="display: none;padding-left:50px;" id="form_bookinterior_clientside_failure">
   Please check your internet connection and try again. We highly recommend using a high-speed broadband Internet connection.
</div>
<div class="clearer">
</div>
<script type="text/javascript">
/* <![CDATA[ */
if (window.CSP_OK) {
      document.observe('dom:loaded', function(e) {
         blockHandler = false;
    window['form_bookinterior_interiorfile'] = new AjaxFixedFormUpload(
        'form_bookinterior_interiorfile', 'form_bookinterior_upload_tsw_setup_book_interior_tsw_book_interior__upload', 'form_bookinterior', '123780522', 'tsw_setup_book_interior_tsw_book_interior__upload', '/title/ajax/paperback/8871655/setup/book_interior/validateupload',
        '/title/ajax/paperback/8871655/setup/book_interior/confirmupload', 'Uploading Interior File', 'Processing Interior File', '*.pdf; *.doc; *.docx; *.rtf', '', '419430400 B',
        universalAjaxHandler.handleOnClick.bind(universalAjaxHandler, [['element', 'tsw_setup_book_interior_tsw_book_interior__upload_results', 'show'], ['element', 'tsw_setup_book_interior_tsw_book_interior__upload_results', 'update', 'Your Interior File cannot exceed 400 MB.'], ['element', 'tsw_setup_book_interior_tsw_book_interior__upload_field_container', 'showError'], ['element', 'tsw_setup_book_interior_tsw_book_interior__upload', 'showError']]), universalAjaxHandler.handleOnClick.bind(universalAjaxHandler, [['element', 'tsw_setup_book_interior_tsw_book_interior__upload_results', 'show'], ['element', 'tsw_setup_book_interior_tsw_book_interior__upload_results', 'update', 'The file you selected is empty. Please select another file.'], ['element', 'tsw_setup_book_interior_tsw_book_interior__upload_field_container', 'showError'], ['element', 'tsw_setup_book_interior_tsw_book_interior__upload', 'showError']]), universalAjaxHandler.handleOnClick.bind(universalAjaxHandler, [['element', 'tsw_setup_book_interior_tsw_book_interior__upload_results', 'update', ''], ['element', 'tsw_setup_book_interior_tsw_book_interior__upload_results', 'hide'], ['element', 'tsw_setup_book_interior_tsw_book_interior__upload_field_container', 'hideError'], ['element', 'tsw_setup_book_interior_tsw_book_interior__upload', 'hideError']]), 'e2fae1cd4365a945d1b85c369ac342bd0fae3d626797cd12', 'a8bfdf6173bb60eff9b42c338610dc5b',
        '', 0, 100, false
    );
    window['form_bookinterior_interiorfile'].customRequestParams = {"fileType":"TSW_BOOK_INTERIOR"};
      });
}
/* ]]> */
</script>
<div class="" id="bookInteriorWarning" style="display: none;">
<div class="bookInteriorWarning">
   <div class="warningTop"></div>
   <div class="warningIcon">
      <span class="warningText">WARNING:</span>
<span class="">Your book's physical properties were updated. Please resubmit your book's interior.</span>
   </div>
   <div class="clearer"></div>
</div><br>
</div>
</div>



</div>
</form>

当我使用

document.querySelector("#tsw_setup_book_interior_tsw_book_interior__uploadTextTarget").value = "test.pdf";

并单击“保存”按钮,它确实显示了上传进度,但是停留在0%,所以,也许是从

获取路径
<input type="text" class="contentTextField formItemNormal" id="tsw_setup_book_interior_tsw_book_interior__uploadTextTarget" style="width: 200px; margin-top: 5px;" disabled="true">

但是,这将是一个安全问题吗?在该值中设置完整路径也无济于事,它再次停留在0%。

也许他们正在使用AJAX文件上传?

我有点迷失了。 有什么方法可以自动执行此上传?

任何帮助将不胜感激! 谢谢!

编辑:-如果上述操作不可行,是否有任何方法可以使用Puppeteer处理文件对话框?

0 个答案:

没有答案