首页 > 解决方案 > 无法使用 PhantomJS 上传文件

问题描述

这是我的代码:

setTimeout(function () {
    console.log("Page opened, ready to upload file.  Status is", status);
    var inputEl = document.querySelectorAll("input[type=file]")[0];
    console.log("Upload=" + JSON.stringify(inputEl)); // <---- NULL here
    page.upload(inputEl, 'C:/temp/1.JPG'); // <--- so this throws error
    page.evaluate(function () {
                var inputEl = document.querySelectorAll("input[type=file]")[0]; // <--- Not NULL here but...
                page.upload(inputEl, 'C:/temp/1.JPG'); // <--- cannot call page.upload from inside page.evaluate
    });
}, 5000);

正如这里的评论所述,问题是我无法让程序上传文件(实际上是图像),我的代码可能有什么问题?

更新

这是我尝试的另一个代码:

inputEl = page.evaluate(function(s) {
                  console.log("Selector=" + s);
                  var el = document.querySelectorAll(s)[0];
                  //console.log("(1)Upload=" + JSON.stringify(el));
                  return el;
              }, 'input[type=file]');
              console.log(inputEl.tagName); // output = INPUT
              console.log(inputEl.id); // output = id of element, OK
              page.uploadFile(inputEl.id, "C:\\temp\\1.JPG");

标签: javascriptphantomjs

解决方案


推荐阅读