CasperJS不能正确触发动态脚本/链接调用的回调

CasperJS does not trigger properly on callback of dynamic script/link call

本文关键字:链接 调用 回调 脚本 动态 不能 CasperJS      更新时间:2023-09-26

我有一个问题与Casper JS和一个示例html页面。

我动态添加的外部引导调用上的onLoad事件不会在casper端触发/处理。在我的例子中,屏幕截图永远不会是红色的,但如果我从浏览器加载页面,那么页面看起来确实是红色的。

<html>
<head>
<title>Title of the document</title>
</head>
<body>
<script type="text/javascript">
    var s = document.createElement("link");
    s.href = "http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css";
    s.rel = "stylesheet";
    s.type = "text/css";
    document.body.appendChild(s);
    s.onload = function(){
        document.body.style.backgroundColor = "red";
    };
</script>
</body>
</html>

下面是casper调用的文件:

var casper = require('casper').create();
casper.start('test.html', function() {
});
casper.wait(5000, function(){
    casper.capture("debug.png");
});
casper.run(function() {
});

我错过了什么吗?

PhantomJS (v1.9.7), CasperJS的底层无头浏览器,具有与Chrome 13相同的功能,似乎不支持onload上的link元素。如果你需要它,你可以在这里找到一些解决方法。

否则,下面的代码可以满足您的要求:

casper.start('test.html');
casper.waitForResource(/css/, function(){
    this.evaluate(function(){
        document.body.style.backgroundColor = "red";
    });
    this.capture("debug.png");
});

你可以使用--enginge=slimerjs和http://slimerjs.org/从你安装的firefox运行gecko引擎。

在一个无关的笔记上。link元素更适合head而不是body