У меня проблемы с запуском jQuery в PhantomJS. Я нашел этот ответ, в котором говорится о том, что нет переменная доступна внутри функции оценки, но вопрос касается модуля узла, и в моем примере я вызываю только console.log
внутри функции оценки. Я также разместил этот вопрос на GitHub.
Ранее для некоторых страниц следующий код evaluate
не выполнялся. Теперь, когда @b1f56gd4 оказал некоторую помощь, теперь он печатает сообщения; Я не могу его выполнить, но теперь я вижу это:
Страница https://login.yahoo.com/ содержала небезопасное содержимое с http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js.
Я не могу загрузить jQuery из другого домена, и параметры --local-to-remote-url-access=true
или --web-security=false
не имеют значения.
Я попробую загрузить jQuery локально. Вот код:
console.log('Loading a web page');
var url = 'https://login.yahoo.com/';
var page = require('webpage').create();
console.log('Setting error handling');
page.onConsoleMessage = function (msg) {
console.log(msg);
};
page.onError = function (msg, trace) {
console.log(msg);
trace.forEach(function(item) {
console.log(' ', item.file, ':', item.line);
})
phantom.exit();
}
console.log('Error handling is set');
console.log('Opening page');
page.open(url, function (status) {
if (status != 'success') {
console.log('F-' + status);
} else {
console.log('S-' + status);
//-------------------------------------------------
var jsLoc = '';
jsLoc = 'jquery.min.js'; // to load local
//jsLoc = 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'; // to load remote
var func = function(pg){
console.log('Function called');
console.log('Page evaluating');
console.log(pg);
pg.evaluate(function() {
console.log('Page evaluate started');
//---
var loginVar = '[email protected]';
var pwdVar = 'itsmypass_445f4hd564hd56f46s';
//---
$("#login_form #username").value = loginVar;
$("#login_form #passwd").value = pwdVar;
//---
});
console.log('Rendering');
pg.render('ystsA.png');
console.log('Rendered');
}
if (typeof jQuery == 'undefined') {
console.log('JQuery Loading'); // <<<<==== Execute only until here
console.log('Source:['+jsLoc+']');
var rs = page.includeJs(jsLoc, function() // <<<<===== Fail here, jsLoc was changed to load locally and after tried remotely, i tried use page.injectJs but fail too
{
console.log('JQuery Loaded'); // <<<< ===== Never reach here, no matter if loading local or remote script in include above
func(page);
});
page.render('ystsB.png');
} else {
console.log('JQuery Already Loaded');
func(page);
page.render('ystsC.png');
}
//-------------------------------------------------
}
phantom.exit();
});
Прочитав ответ @ g4d564w56, я сделал все без JQuery, затем я могу заполнить текстовое поле, но не могу нажать кнопку, чтобы опубликовать сообщение в форме входа.
См. Новый код:
console.log('Loading a web page');
var url = 'https://login.yahoo.com/';
var page = require('webpage').create();
console.log('Setting error handling');
page.onConsoleMessage = function (msg) {
console.log(msg);
};
page.onError = function (msg, trace) {
console.log(msg);
trace.forEach(function(item) {
console.log(' ', item.file, ':', item.line);
})
phantom.exit();
}
console.log('Error handling is set');
console.log('Opening page');
page.open(url, function (status) {
if (status != 'success') {
console.log('F-' + status);
} else {
console.log('S-' + status);
//-------------------------------------------------
var jsLoc = '';
jsLoc = 'jquery.min.js'; // to load local
//jsLoc = 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'; // to load remote
var act01 = function(pg){
console.log('Function called');
console.log('Page evaluating');
console.log(pg);
pg.evaluate(function() {
var getElmById = function(id){
return document.getElementById(id);
}
console.log('Page evaluate started');
//---
var loginVar = '[email protected]';
var pwdVar = 'itsmypass_445f4hd564hd56f46s';
//---
getElmById("username").value = loginVar;
getElmById("passwd").value = pwdVar;
getElmById("login_form").submit(); /// <<<<==== now its dont work !!!
//---
});
console.log('Rendering');
pg.render('ystsA.png');
console.log('Rendered');
}
act01(page);
//-------------------------------------------------
}
phantom.exit();
});