phantomjs:我的page.evaluate(function())没有接受参数,我将其作为值传递并给了我空错误

问题描述 投票:1回答:1

我的代码如下:

var page = new WebPage(), testindex = 0, loadInProgress = false;
var fs = require('fs');        
var sheet = fs.read('courtcalllist.csv').split("\n").map(function(row){
    return row.split(",");});



page.onConsoleMessage = function(msg) {
    console.log(msg);
};

page.onLoadStarted = function() {
    loadInProgress = true;
    console.log("load started");
};

page.onLoadFinished = function() {
    loadInProgress = false;
    console.log("load finished");
};


for (var i = 1; i < sheet.length - 1; i ++ ){ 

        year = sheet[i][8];
        district = sheet[i][9];
        casenumber = sheet[i][10];
var steps = [
    function () {//first function to open page 
        page.open("http://www.cookcountyclerkofcourt.org/CourtCaseSearch/CourtCallSearch.aspx");
    }, //first function bracket

    function () {// second function calls page to evaulate page input 

         page.evaluate(function(year, district, casenumber) {  
                    console.log("this is my test " + year, district, casenumber);

                    document.getElementById('ctl00_MainContent_txtCaseYear').value = year;  //'2018';
                    document.getElementById('ctl00_MainContent_txtCaseCode').value =  district;//'M3';
                    document.getElementById('ctl00_MainContent_txtCaseNumber').value = casenumber;//'005338';
                    return;

        },year, district, casenumber);

     }, //2nd function bracket
     function () { // 3rd function calls evaluate click 
        //click 
        page.evaluate(function() {

            document.getElementById('ctl00_MainContent_btnSearch').click();

        });
      }, //3rd function bracket 
      function  () { // 4th function calls the 2nd page after data
        // Output content of page to stdout after form has been submitted
        page.evaluate(function() {
            for (i = 1; i < 11; i++ ){
            console.log(document.querySelectorAll('td')[i].innerHTML);
        }
        });
    } //4thbracket 

];

    interval = setInterval(function() {
      if (!loadInProgress && typeof steps[testindex] == "function") {
        console.log("step " + (testindex + 1));
        steps[testindex]();
        testindex++;
      }
      if (typeof steps[testindex] != "function") {
        console.log("test complete!");
        phantom.exit();
      }
    }, 50);

}

由于某种原因,console.log将这样打印我传递的变量:

this is my test 2018 M3 005338

这是正确的,这就是我需要的,但是在对他们进行评估时,我得到了:

TypeError: null is not an object (evaluating 'document.getElementById('ctl00_MainContent_txtCaseYear').value = year')

  undefined:3

如果我用硬代码编码上面的值,上面的代码将起作用,这就是注释掉的内容,但是它不会接受我给它的变量。

我的变量是否已从字符串转换为对象,因此无法读取?我在这里茫然,我什至尝试过

                        document.getElementById('ctl00_MainContent_txtCaseYear').value = year.toString;
                        document.getElementById('ctl00_MainContent_txtCaseCode').value =  district.toString;
                        document.getElementById('ctl00_MainContent_txtCaseNumber').value = caenumber.toString;

它只是给了我同样的错误。

javascript parameters phantomjs parameter-passing submit
1个回答
1
投票
function () {
        page.evaluate(function(string1, string2, string3) {
             console.log("this is my test " + String(string1) + String(string2)  + String(string3));//, district, casenumber);
             document.getElementById('ctl00_MainContent_txtCaseYear').value = String(string1);//'2002';
            // console.log("this is my test " + String(string2));
             document.getElementById('ctl00_MainContent_txtCaseCode').value =  String(string2);//'M1';//
           //  console.log("this is my test " + String(string3));
             document.getElementById('ctl00_MainContent_txtCaseNumber').value = String(string3);//'144115'; // 
             return;
    },year, district, casenumber);

是如何传递变量。注意参数放在最后,并且必须在function()中将它们写为其他形式。

© www.soinside.com 2019 - 2024. All rights reserved.