2014-09-29 82 views
-1

我試圖用phantomjs 例如寫javascript:如何使用與phantomjs

var webPage = require('webpage'); 
var page = webPage.create(); 
page.open('http://www.w3school.com.cn', 
function() { 
    var res = page.evaluate(function() { 
     return document.getElmentsByTagName('a'); 
    }); 
    console.log(res); 
    phantom.exit(); 
}); 

乳寧,錯誤提示:

TypeError: 'undefined' is not a function (evaluating 'document.getElmentsByTagName('a')') 

    phantomjs://webpage.evaluate():2 
    phantomjs://webpage.evaluate():3 
    phantomjs://webpage.evaluate():3 
null 

爲什麼我做了什麼?

+0

我在代碼中看不到'document.getElmentById',但只有'getElmentsByTagName'。他們都有錯別字。它應該是'getElementById'或'getElementsByTagName'。請顯示與錯誤消息相匹配的代碼。 – 2014-09-29 06:08:04

+0

你可以嘗試閱讀狀態嗎?例如:'page.open('...',function(status){' 'console.log(status);' 「success''。或者是錯誤的錯誤? – 2014-09-29 07:16:39

+0

如你所願,成功但仍然報告TypeError:'undefined'不是一個函數... @ AgashThamo。 – noob123 2014-09-29 07:27:46

回答

0

看起來你不打電話phantom.exit();當Phantomjs完成它的事情。

將上述命令追加到腳本的末尾,它應該正常工作。

+0

傾聽您的意見,仍然會報告錯誤。 – noob123 2014-09-29 04:37:36

+0

腳本沒有結束怎麼辦與明顯的拼寫錯誤有關。這應該是一個評論。 – 2014-09-29 06:11:23

+0

一個給定的花紋代碼。 – Pytth 2014-09-29 13:51:47