使用PhantomJS设置远程调试

35 phantomjs

我正在尝试使用PhantomJS设置远程调试,没有太多运气.我按照https://github.com/ariya/phantomjs/wiki/Troubleshooting上的说明进行操作.我有一个名为的小程序debug.js:

var system  = require('system' ), fs = require('fs'), webpage = require('webpage');

(function(phantom){
    var page=webpage.create();

    function debugPage(){
        console.log("Refresh a second debugger-port page and open a second webkit inspector for the target page.");
        console.log("Letting this page continue will then trigger a break in the target page.");
        debugger; // pause here in first web browser tab for steps 5 & 6
        page.open(system.args[1]);
        page.evaluateAsync(function() {
            debugger; // step 7 will wait here in the second web browser tab
        });
    }
    debugPage();
}(phantom));
Run Code Online (Sandbox Code Playgroud)

现在我从命令行运行它:

$ phantomjs --remote-debugger-port=9001 --remote-debugger-autorun=yes debug.js my.xhtml
Run Code Online (Sandbox Code Playgroud)

console.log消息现在显示在shell窗口.我打开一个浏览器页面localhost:9001.正是在这一点上,文档说"获取幻像上下文的第一个web检查器"但是,我只看到一个条目about:blank.当我点击它时,我会得到一个检查员,其中包含无关的:空白页面和URL http://localhost:9001/webkit/inspector/inspector.html?page=1.文档讨论了执行__run(),但我似乎无法进入我会这样做的页面; about:html看起来像是一个__run()无操作者.

FWIW,我在W8下使用PhantomJS 1.9.1.

我错过了什么?

Cyb*_*axs 23

文件说:

要运行脚本,只需在Web Inspector控制台中输入__run()命令即可.

__run()不是一个无操作,而只是你的脚本的包装.您需要先选择Console选项卡,然后__run()在命令窗口中输入.如果您熟悉Chrome,那么它与开发人员工具相当.

调试控制台

  • 你也可以根据https://github.com/ariya/phantomjs/wiki/Troubleshooting做--remote-debugger-autorun = yes (3认同)
  • 谢谢!在故障排除文档页面上看不到__run()之前的_double下划线... ... http://phantomjs.org/troubleshooting.html (2认同)

Bra*_*rks 21

要调试脚本,请启动phantomjs,如下所示:

phantomjs --remote-debugger-port=9000 hello.js
Run Code Online (Sandbox Code Playgroud)

这是一个非常简单的测试脚本(hello.js).请注意,您应该放在debugger;脚本的顶部,或者要放在脚本中的任何位置,以便进入调试器.

hello.js

debugger;

for (var i=0; i < 5; i++)
{
  console.log('debugging in phantom js:' + i);
}

phantom.exit();
Run Code Online (Sandbox Code Playgroud)

现在只需在浏览器中加载以下网址:

http://127.0.0.1:9000/

然后,您将在浏览器页面中看到一个链接

about:blank
Run Code Online (Sandbox Code Playgroud)

点击它,然后您会看到整个页面看起来像Chrome Inspector.单击此页面中工具栏中的"控制台"按钮(不是您习惯使用的Chrome或Safari控制台).

现在,在该控制台类型中__run()点击回车.您的脚本将显示并开始调试!


Ast*_*ear 10

我使用Chrome版本57.0.2987.133(64位)在Mac上进行调试时遇到问题.我在localhost上打开调试器:9000(127.0.0.1:9000对我不起作用)但是在输入__run()之后(是的,使用双下划线),没有响应.我可以在Sources下看到其他js文件,我的列表但是是空的.(我确实启用了chrome调试)

我在safari上尝试了同样的方法,这一切都像宣传的一样.

Chrome更新:(来自下面的Thiago Fernandes):显然问题是Chrome不接受回车键引起的,因此解决方法是在Chrome控制台内评估此功能,以使enterKey正常工作:

function isEnterKey(event) { return (event.keyCode !== 229 && event.keyIdentifier === "Enter") || event.keyCode === 13; } 
Run Code Online (Sandbox Code Playgroud)

  • 解决方法是在chrome控制台内部评估此函数,因此enterKey开始工作:```function isEnterKey(event){return(event.keyCode!== 229 && event.keyIdentifier ==="Enter")|| event.keyCode === 13; }``` (2认同)

Leg*_*nds 5

就我而言,__run()不会在控制台中执行。如果您遇到同样的问题,请继续阅读......

打开 PowerShell 并执行脚本:

cls
# we go to the folder where our test.js script resides
cd "C:\Users\xxx\Phantomjs.Console"
phantomjs --remote-debugger-port=9000 --remote-debugger-autorun=yes test.js 
Run Code Online (Sandbox Code Playgroud)
  1. 打开 Chrome 浏览器并转到
  2. http://本地主机:9000
  3. 单击您的(在我的例子中是 test.js 文件)。
  4. 切换到“来源”选项卡!
  5. __run()在 Watch Expressions 下执行

debugger在脚本中添加一条语句以进行调试!

在此输入图像描述