PhantomJS - 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('https:///tasks/alert.html', function(status) { 
   //wpage.stop(); 
   console.log(status);  
   
   phantom.exit(); 
});

alert.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 
phantomjs_webpage_module_events_callbacks.htm
廣告
© . All rights reserved.