PhantomJS - 方法



PhantomJS 是一個無需瀏覽器即可執行 JavaScript 的平臺。為此,它使用了以下方法,這些方法有助於新增 Cookie、刪除、清除、退出指令碼、注入 JS 等。

本章將詳細討論這些 PhantomJS 方法及其語法。類似的方法,即 **addcookie、injectjs** 存在於 webpage 模組中,將在後續章節中討論。

PhantomJS 提供以下方法,可以幫助我們在沒有瀏覽器的情況下執行 JavaScript:

  • addCookie
  • clearCookie
  • deleteCookie
  • exit
  • injectJs

現在讓我們用示例詳細瞭解這些方法。

addCookie

addcookie 方法用於新增 Cookie 並將其儲存在資料中。這與瀏覽器儲存 Cookie 的方式類似。它接受一個引數,該引數是一個包含所有 Cookie 屬性的物件,其語法如下所示:

語法

其語法如下:

phantom.addCookie ({ 
   "name" : "cookie_name",  
   "value" : "cookie_value", 
   "domain" : "localhost" 
});

name、value 和 domain 是必須新增到 addcookie 函式的屬性。如果 Cookie 物件中缺少任何這些屬性,則此方法將失敗。

  • **name** - 指定 Cookie 的名稱。

  • **value** - 指定要使用的 Cookie 的值。

  • **domain** - 將應用 Cookie 的域名。

示例

這是一個 **addcookie** 方法的示例。

var page = require('webpage').create(),url = 'https:///tasks/a.html'; 
page.open(url, function(status) { 
   if (status === 'success') {     
      phantom.addCookie({   //add name cookie1 with value = 1 
         name: 'cookie1', 
         value: '1', 
         domain: 'localhost' 
      }); 
      phantom.addCookie({   // add cookie2 with value 2 
         name: 'cookie2', 
         value: '2', 
         domain: 'localhost' 
      }); 
      phantom.addCookie({   // add cookie3 with value 3 
         name: 'cookie3', 
         value: '3', 
         domain: 'localhost' 
      }); 
      console.log('Added 3 cookies'); 
      console.log('Total cookies :'+phantom.cookies.length);  
      
      // will output the total cookies added to the url.    
   } else { 
      console.error('Cannot open file'); 
      phantom.exit(1); 
   } 
}); 

示例

a.html

<html>
   <head>
      <title>Welcome to phantomjs test page</title>
   </head>
   
   <body>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
   </body>
</html>

上述程式生成以下**輸出**。

Added 3 cookies 
Total cookies :3

程式碼註釋不言自明。

clearCookies

此方法允許刪除所有 Cookie。

語法

其語法如下:

phantom.clearCookies();

此概念類似於透過選擇瀏覽器選單中的選項來刪除瀏覽器 Cookie。

示例

這是一個 **clearCookies** 方法的示例。

var page = require('webpage').create(),url = 'https:///tasks/a.html'; 
page.open(url, function(status) { 
   if (status === 'success') {     
      phantom.addCookie({   //add name cookie1 with value = 1 
         name: 'cookie1', 
         value: '1', 
         domain: 'localhost' 
      }); 
      phantom.addCookie({   // add cookie2 with value 2 
         name: 'cookie2', 
         value: '2', 
         domain: 'localhost' 
      }); 
      phantom.addCookie({   // add cookie3 with value 3 
         name: 'cookie3', 
         value: '3', 
         domain: 'localhost' 
      }); 
      console.log('Added 3 cookies'); 
      console.log('Total cookies :'+phantom.cookies.length); 
      phantom.clearCookies(); 
      console.log(
         'After clearcookies method total cookies :' +phantom.cookies.length); 
      
      phantom.exit();     
   } else { 
      console.error('Cannot open file'); 
      phantom.exit(1); 
   } 
}); 

a.html

<html>
   <head>
      <title>Welcome to phantomjs test page</title>
   </head>
   
   <body>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
      <h1>This is a test page</h1>
   </body>
</html>

上述程式生成以下**輸出**。

Added 3 cookies 
Total cookies :3 
After clearcookies method total cookies :0 

deleteCookie

刪除 **CookieJar** 中任何具有與 cookieName 匹配的 'name' 屬性的 Cookie。如果成功刪除,則返回 **true**;否則返回 **false**。

語法

其語法如下:

phantom.deleteCookie(cookiename);

讓我們透過一個示例來了解 **addcookie、clearcookies** 和 **deletecookie**。

示例

這是一個演示 deleteCookie 方法用法的示例:

檔案:cookie.js

var page = require('webpage').create(),url = 'https:///tasks/a.html'; 
page.open(url, function(status) { 
   if (status === 'success') {     
      phantom.addCookie({   //add name cookie1 with value = 1 
         name: 'cookie1', 
         value: '1', 
         domain: 'localhost' 
      }); 
      phantom.addCookie({   // add cookie2 with value 2 
         name: 'cookie2', 
         value: '2', 
         domain: 'localhost' 
      });
      phantom.addCookie({   // add cookie3 with value 3 
         name: 'cookie3', 
         value: '3', 
         domain: 'localhost' 
      });  
      console.log('Added 3 cookies'); 
      console.log('Total cookies :'+phantom.cookies.length); 
      
      //will output the total cookies added to the url.    
      console.log("Deleting cookie2"); 
      phantom.deleteCookie('cookie2'); 
      
      console.log('Total cookies :'+phantom.cookies.length); 
      phantom.clearCookies();
      
      console.log(
         'After clearcookies method total cookies :' +phantom.cookies.length); 
      phantom.exit(); 
   } else { 
      console.error('Cannot open file'); 
      phantom.exit(1); 
   } 
});

上述程式生成以下**輸出**。

phantomjs cookie.js
Added 3 cookies
Total cookies :3
Deleting cookie2
Total cookies :2
After clearcookies method total cookies :0

exit

phantom.exit 方法將退出它啟動的指令碼。它使用提到的返回值退出程式。如果沒有傳遞值,則返回 **‘0’**。

語法

其語法如下:

phantom.exit(value);

如果您不新增 **phantom.exit**,則命令列會假設執行仍在進行中,並且不會完成。

示例

讓我們來看一個示例,以瞭解 **exit** 方法的用法。

console.log('Welcome to phantomJs');  // outputs Welcome to phantomJS 
var a = 1; 
if (a === 1) { 
   console.log('Exit 1'); //outputs Exit 1 
   phantom.exit(); // Code exits. 
} else { 
   console.log('Exit 2'); 
   phantom.exit(1); 
}

上述程式生成以下**輸出**。

phantomjs exit.js

Welcome to phantomJs 
Exit 1 

phantom.exit 之後的所有程式碼段都不會執行,因為 phantom.exit 是結束指令碼的方法。

injectJs

injectJs 用於在 phantom 中新增 **addtionaljs** 檔案。如果當前 **directory librarypath** 中找不到該檔案,則 phantom 屬性 (phantom.libraryPath) 將用作跟蹤路徑的其他位置。如果檔案新增成功,則返回 **true**;否則返回 **false**,表示失敗,因為它無法找到該檔案。

語法

其語法如下:

phantom.injectJs(filename);

示例

讓我們來看下面的示例,以瞭解 **injectJs** 的用法。

檔名:inject.js

console.log(“Added file”); 

檔名:addfile.js

var addfile =  injectJs(inject.js);
console.log(addfile);
phantom.exit();

輸出

**命令** - C:\phantomjs\bin>phantomjs addfile.js

Added file // coming from inject.js
true

在上面的示例中,**addfile.js** 使用 injectJs 呼叫檔案 **inject.js**。當您執行 addfile.js 時,inject.js 中的 console.log 將顯示在輸出中。它還為 addfile 變數顯示 true,因為 inject.js 檔案已成功新增。

廣告
© . All rights reserved.