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

查看:94
本文介绍了在PhantomJS上,我不能包含jQuery,没有jQuery,我不能发布表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在PhantomJS中运行jQuery.我找到了答案,其中提到没有变量在评估函数内部可用,但问题是关于节点模块的,在我的示例中,我仅在评估函数内部调用console.log.我也将此问题放在了 GitHub上.

I am having trouble running jQuery in PhantomJS. I have found this answer, which talks about no variable is available inside evaluate function but the question is about a node module and on my example I only call console.log inside evaluate function. I have put this question on GitHub too.

以前,对于某些页面,以下evaluate代码未执行.现在,@ b1f56gd4提供了一些帮助,它现在可以打印消息了.我无法执行它,但是现在我可以看到:

Previously, for some pages, the following evaluate code didn't execute. Now that @b1f56gd4 has provided some help, it now prints messages; I can't execute it but now I can see this:

https://login.yahoo.com/上的页面运行了来自 http://ajax.googleapis.com/ajax/libs/jquery /1.6.1/jquery.min.js .

The page at https://login.yahoo.com/ ran insecure content from http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js.

我无法从其他域加载jQuery,并且--local-to-remote-url-access=true--web-security=false选项没有区别.

I can't load jQuery from different domain and the --local-to-remote-url-access=true or --web-security=false options make no difference.

我将尝试在本地加载jQuery.这是代码:

I will try load jQuery locally. Here is the code:

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就做了所有操作,然后我可以填写文本框,但不能单击按钮以在登录表单上发布. 查看新代码:

After reading @g4d564w56 answer i did all without JQuery then i can fill textbox but cant click on button to post on login form.
See the new code:

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();
});

推荐答案

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

I know this question has already been answer about a year ago, but the answer didn't really address the issue. The reason for the error below:

"上的内容不安全 http://ajax.googleapis.com/ajax/libs /jquery/1.6.1/jquery.min.js ."

"The page at https://login.yahoo.com/ ran insecure content from 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 ,此错误将消失.花了一段时间才能弄清楚.

Is that the login page is an https page and you're trying to load an http resource. If you change the url to https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js this error will go away. Took a while to figure that out.

这篇关于在PhantomJS上,我不能包含jQuery,没有jQuery,我不能发布表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆