ron*_*ils 4 javascript ajax jquery phantomjs
我是 phantom.js 的新手,我试图在网站页面上导航,点击一个链接(调用 AJAX 函数并更改文档 HTML)与 phantom.js。
这是我的代码:
window.setTimeout(function(){
phantom.exit();
}, 120000);
var page = require('webpage').create();
page.open("http://example.com", function(status) {
if (status !== 'success') {
console.log('{"error":"Unable to load the address for page"}');
phantom.exit();
}
var action = page.evaluate(function() {
document.getElementById("anID").click();
return "clicked";
});
var results = page.evaluate(function() {
return document.documentElement.innerHTML;
});
console.log(action);
window.setInterval(function() {
console.log(results);
phantom.exit();
}, 3000);
});
Run Code Online (Sandbox Code Playgroud)
我很困惑,因为在我的“动作”函数中,click() 调用引发了重复 3 次的错误:
TypeError: 'undefined' 不是函数
phantomjs://webpage.evaluate():3 phantomjs://webpage.evaluate():1
ph.js:121 nullTypeError: 'undefined' 不是函数
phantomjs://webpage.evaluate():3 phantomjs://webpage.evaluate():1
ph.js:121 nullTypeError: 'undefined' 不是函数
phantomjs://webpage.evaluate():3 phantomjs://webpage.evaluate():1
ph.js:121 null
此外,如果我在发送点击时评论该行,则操作功能不再引发错误并返回“点击”控制台日志。但是3次...
我究竟做错了什么 ?
提前致谢。
我终于设法用这段代码完成了我的任务。我跳过了点击动作,直接进入了 AJAX 调用:
// phantomjs test.js 'http://www.example.com' 'anID'
var system = require('system');
var page = require('webpage').create();
var url = system.args[1];
var target = system.args[2];
page.onConsoleMessage = function (msg) {
console.log(msg);
phantom.exit();
};
page.open(url, function (status) {
function evaluate(page, func) {
var args = [].slice.call(arguments, 2);
var fn = "function() { return (" + func.toString() + ").apply(this, " + JSON.stringify(args) + ");}";
return page.evaluate(fn);
}
page.injectJs('jquery-1.7.2.min.js');
if (status === 'success') {
evaluate(page, function(target) {
$.ajax({
type: 'POST',
url: document.URL,
data: "__EVENTTARGET=" + target,
success: function(msg){
console.log(msg);
}
});
}, target);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7699 次 |
| 最近记录: |