为什么我没有收到phantomjs页面响应? [英] Why am I not receiving phantomjs page response?

查看:84
本文介绍了为什么我没有收到phantomjs页面响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用phantomjs(在Windows上为1.9.1版)访问一些QUnit测试url.我在公司站点的代理后面,但是要尝试访问的URL是从本地开发工作站提供的,而且我什至尝试使用其他两个浏览器(Hv3和Dooble)访问相同的URL,而不必要代理设置,即使它们无法执行QUnit javascript,它们也会获得HTML响应.

I'm trying to access some QUnit test urls using phantomjs (version 1.9.1, on Windows). I'm behind a proxy at a corporate site but the URLs I'm trying to access are being served from my local development workstation, plus I've even tried accessing the same URLs with two other browsers (Hv3 and Dooble) without the necessary proxy settings, and they get an HTML response, even if they can't execute the QUnit javascript.

因此,我什至尝试将javascriptEnabled设置(加上其他几个设置,请参见下面的代码)调整为false,以尝试仅获取原始HTML,但无济于事.我已经将对page.open的调用包装在try/catch中,但是显然这并不是因为异常;而不是最后一个phantom.exit()语句执行之前的console.log语句.

So I've even tried adjusting the javascriptEnabled setting (plus another couple of settings, see code below) to false to try to just get the raw HTML, but to no avail. I've wrapped my call to page.open in a try/catch but apparently this is not because of an exception; rather a console.log statement immediately before the final phantom.exit() statement gets executed.

此外,我还遵循了 https://github.com/ariya/phantomjs的建议/wiki/Network-Monitoring ,包括从page.onResourceRequested,page.onError和page.onResourceReceived进行日志记录,仅执行onResourceReceived的回调.而且我指定了--proxy-type = none命令行参数,但无济于事.

Furthermore I've followed recommendations from https://github.com/ariya/phantomjs/wiki/Network-Monitoring including logging from page.onResourceRequested, page.onError and page.onResourceReceived, and only the callback for onResourceReceived gets executed. And I'm specifying the --proxy-type=none command line argument, all to no avail.

下面的代码和输出,谢谢.我不知所措;也许这是一个phantomjs问题?只是想在报告之前排除一切.

Code and output below, thanks in advance. I'm at a loss; maybe it's a phantomjs issue? Just want to rule out everything before reporting it though.

代码:

var page = require('webpage').create();

page.onResourceRequested = function (request) {
    console.log('Request ' + JSON.stringify(request, undefined, 4));
};

page.onResourceReceived = function (response) {
    console.log('Receive ' + JSON.stringify(response, undefined, 4));
};

page.onError = function (msg, trace) {
    console.log(msg);
    trace.forEach(function(item) {
        console.log('  ', item.file, ':', item.line);
    })
}

page.settings.webSecurityEnabled = false;
page.settings.localToRemoteUrlAccessEnabled = true;
//page.settings.javascriptEnabled = false;

for (var setting in page.settings) {
    console.log(setting + ": " + page.settings[setting]);
}

try {
    page.open('http://local.example.com:9001/test/workflow', function() {
        console.log('page opened');
    });
}
catch(xcep) {
    console.log(xcep);
}

console.log('before exit');
phantom.exit();

输出:

XSSAuditingEnabled: false
javascriptCanCloseWindows: true
javascriptCanOpenWindows: true
javascriptEnabled: true
loadImages: true
localToRemoteUrlAccessEnabled: true
userAgent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.1 Safari/534.34
webSecurityEnabled: false
Request {
    "headers": [
        {
            "name": "User-Agent",
            "value": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.1 Safari/534.34"
        },
        {
            "name": "Accept",
            "value": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
        }
    ],
    "id": 1,
    "method": "GET",
    "time": "2013-07-12T09:49:58.262Z",
    "url": "http://local.example.com:9001/test/workflow"
}
before exit

推荐答案

page.open()是异步的.因此带有phantom.exit()的行将在页面加载之前执行,这将停止PhantomJS进程.将phantom.exit()移动到page.open()的回调中,并将其放在回调的末尾(即,在处理代码之后).因此,基本上您将拥有:

page.open() is asynchronous. So the line with phantom.exit() will be executed before the page has been loaded and this stops the PhantomJS process. Move the phantom.exit() inside the callback for page.open() and place it at the end of the callback (i.e., after your processing code). So basically you will have this:

page.open('http://local.example.com:9001/test/workflow', function() {
      console.log('page opened');

      phantom.exit();
});

这篇关于为什么我没有收到phantomjs页面响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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