PhantomJS ``onAlert()`` 回调:监听页面弹窗提示消息


发布日期 : 2024-07-22 14:32:52 UTC

访问量: 10 次浏览

PhantomJS onAlert()

onAlert() 回调函数在页面上出现提示框时被调用。回调函数接受一个字符串参数,不返回任何值。

语法

它的语法如下所示−

var wpage = require('webpage').create();
wpage.onAlert = function(msg) {};

示例

以下示例展示了 onAlert() 方法的使用。

var wpage = require('webpage').create();
wpage.onAlert = function(str) {
console.log(str);
}
wpage.open('http://localhost/tasks/alert.html', function(status) {
//wpage.stop();
console.log(status);

phantom.exit();
});

提示 .html

<html>
<head></head>
<body>
<script type = "text/javascript">
alert("Welcome to phantomjs");
</script>
<h1>This is a test page</h1>
</body>
</html>

以上程序生成以下输出

Welcome to phantomjs
Success