在PhantomJS上,我不能包含jQuery,没有jQuery,我就不能发布表单数据。[英] On PhantomJS I can't include jQuery and without jQuery I can't post form data

问题描述

我在 PhantomJS 中运行 jQuery 时遇到问题.我找到了 this 答案,其中谈到没有变量在评估函数中可用,但问题是关于节点模块,在我的示例中,我只在评估函数中调用 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 = 'ih5d4hf65465fd45h6@yahoo.com.br';
                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 = 'ih5d4hf65465fd45h6@yahoo.com.br';
                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();
});

推荐答案

我知道这个问题大约一年前就已经有了答案,但答案并没有真正解决问题.错误原因如下:

<块引用>

"https://login.yahoo.com/ 的页面运行不安全的内容http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js."

登录页面是 https 页面并且您正在尝试加载 http 资源.如果您将网址更改为 https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js 这个错误会消失.花了一段时间才弄清楚.

本文地址:https://www.itbaoku.cn/post/1739904.html