Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法使用PhantomJS上载文件_Javascript_Phantomjs - Fatal编程技术网

Javascript 无法使用PhantomJS上载文件

Javascript 无法使用PhantomJS上载文件,javascript,phantomjs,Javascript,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

这是我的代码:

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);

只有在
页面.evaluate
回调中,您才能访问输入元素,我认为,这意味着回调在加载页面的内部运行(因此您无法访问从您的角度看似乎在范围内的任何变量)。我想你可以尝试设置元素并触发上传,我对超时需求有点好奇,你不是在使用回调等待页面加载吗?之所以添加setTimeout,是因为在此之前单击了登录。更新的代码是正确的,它对你不起作用吗?你怎么知道?代码现在运行了,很明显,一个phantom.exit导致上传提前关闭。
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");