2013-05-11 48 views
42

Trong PhantomJS, webpage.open nhận cuộc gọi lại với thông số trạng thái được đặt thành 'thành công' hoặc 'không thành công'. Theo các tài liệu, nó sẽ là "thành công" nếu không có lỗi mạng nào xảy ra, nếu không 'không thành công'. " Có cách nào để xem lỗi mạng cơ bản gây ra lỗi không?Gỡ lỗi trang web PhantomJS. Lỗi thất bại

url Tôi đang cố gắng tải công trình tốt khi tôi đặt nó trong trình duyệt của mình và khi tôi chụp ảnh màn hình sau khi nhận được thông báo 'không thành công', tôi thấy trang mà tôi đã truy cập trước khi tôi gọi trang web.open (vì vậy tôi không thể bỏ qua thất bại). Tôi đang sử dụng Phantom để thử nghiệm, vì vậy lý tưởng tôi muốn một cách mạnh mẽ dễ dàng nhận được một messsage lỗi hữu ích khi website.open thất bại (hoặc tốt hơn nhưng chưa bao giờ thất bại!)

Trả lời

60

Tìm thấy bài đăng này giải thích cách thiết lập callbacks để có được nguyên nhân sâu xa cho sự thất bại: http://newspaint.wordpress.com/2013/04/25/getting-to-the-bottom-of-why-a-phantomjs-page-load-fails/

Dựa trên các trang đó, bạn có thể in ra lỗi như sau:

page.onResourceError = function(resourceError) { 
    console.error(resourceError.url + ': ' + resourceError.errorString); 
}; 

trang tiếp tục cho thấy một ví dụ về khai thác gỗ chi tiết cho phantoms

page.onResourceRequested = function (request) { 
    system.stderr.writeLine('= onResourceRequested()'); 
    system.stderr.writeLine(' request: ' + JSON.stringify(request, undefined, 4)); 
}; 

page.onResourceReceived = function(response) { 
    system.stderr.writeLine('= onResourceReceived()'); 
    system.stderr.writeLine(' id: ' + response.id + ', stage: "' + response.stage + '", response: ' + JSON.stringify(response)); 
}; 

page.onLoadStarted = function() { 
    system.stderr.writeLine('= onLoadStarted()'); 
    var currentUrl = page.evaluate(function() { 
     return window.location.href; 
    }); 
    system.stderr.writeLine(' leaving url: ' + currentUrl); 
}; 

page.onLoadFinished = function(status) { 
    system.stderr.writeLine('= onLoadFinished()'); 
    system.stderr.writeLine(' status: ' + status); 
}; 

page.onNavigationRequested = function(url, type, willNavigate, main) { 
    system.stderr.writeLine('= onNavigationRequested'); 
    system.stderr.writeLine(' destination_url: ' + url); 
    system.stderr.writeLine(' type (cause): ' + type); 
    system.stderr.writeLine(' will navigate: ' + willNavigate); 
    system.stderr.writeLine(' from page\'s main frame: ' + main); 
}; 

page.onResourceError = function(resourceError) { 
    system.stderr.writeLine('= onResourceError()'); 
    system.stderr.writeLine(' - unable to load url: "' + resourceError.url + '"'); 
    system.stderr.writeLine(' - error code: ' + resourceError.errorCode + ', description: ' + resourceError.errorString); 
}; 

page.onError = function(msg, trace) { 
    system.stderr.writeLine('= onError()'); 
    var msgStack = [' ERROR: ' + msg]; 
    if (trace) { 
     msgStack.push(' TRACE:'); 
     trace.forEach(function(t) { 
      msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : '')); 
     }); 
    } 
    system.stderr.writeLine(msgStack.join('\n')); 
}; 
+2

Đừng quên đánh dấu câu trả lời của riêng bạn là được chấp nhận. – Stephan

+0

Cảm ơn câu trả lời, bạn có thể muốn phác thảo thông tin được đề cập trong liên kết trong trường hợp thông tin bị xóa, mất hoặc thay đổi – Tim

Các vấn đề liên quan