2014-09-29 80 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