Phantomjs-methods

提供:Dev Guides
移動先:案内検索

PhantomJS-メソッド

PhantomJSは、ブラウザーなしでJavaScriptを実行するためのプラットフォームです。 そのために、次のメソッドが使用されます。これらのメソッドは、Cookieの追加、削除、クリア、スクリプトの終了、JSの挿入などに役立ちます。

この章では、これらのPhantomJSメソッドとその構文について詳しく説明します。 同様の方法、すなわち addcookie、injectjs はWebページモジュールに存在します。これについては、以降の章で説明します。

PhantomJSは、ブラウザなしでJavaScriptを実行するのに役立つ次のメソッドを公開します-

  • addCookie
  • clearCookie
  • deleteCookie
  • Exit
  • InjectJS

次に、これらの方法を例とともに詳しく説明します。

addCookie

addcookieメソッドは、Cookieを追加してデータに保存するために使用されます。 これは、ブラウザが保存する方法に似ています。 それは、Cookieのすべてのプロパティを持つオブジェクトである単一の引数を取り、その構文は以下のようになります-

構文

その構文は次のとおりです-

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

名前、値、ドメインは、addcookie関数に追加される必須プロパティです。 このプロパティのいずれかがCookieオブジェクトにない場合、このメソッドは失敗します。

  • name -Cookieの名前を指定します。
  • -使用するCookieの値を指定します。
  • domain -Cookieが適用されるドメイン。

*addcookie* メソッドの例を次に示します。
var page = require('webpage').create(),url = 'http://localhost/tasks/al';
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);
   }
});

al

<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>

上記のプログラムは、次の output を生成します。

Added 3 cookies
Total cookies :3

コードのコメントは一目瞭然です。

クッキーを消す

この方法では、すべてのCookieを削除できます。

構文

その構文は次のとおりです-

phantom.clearCookies();

この概念は、ブラウザーメニューで選択してブラウザーCookieを削除するのと同じように機能します。

*clearCookies* メソッドの例を次に示します。
var page = require('webpage').create(),url = 'http://localhost/tasks/al';
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);
   }
});

al

<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>

上記のプログラムは、次の output を生成します。

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

deleteCookie

cookieNameに一致する「name」プロパティを持つ CookieJar のCookieを削除します。 正常に削除された場合、 true を返します。それ以外の場合は false

構文

その構文は次のとおりです-

phantom.deleteCookie(cookiename);

例の助けを借りて addcookie、clearcookies および deletecookie を理解しましょう。

deleteCookieメソッドの使用方法を示す例があります-

  • ファイル:cookie.js *
var page = require('webpage').create(),url = 'http://localhost/tasks/al';
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);
   }
});

上記のプログラムは、次の output を生成します。

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);
}

上記のプログラムは、次の output を生成します。

*phantomjs exit.js*
Welcome to phantomJs
Exit 1

phantom.exitはスクリプトを終了するメソッドであるため、phantom.exitの後のコードは実行されません。

injectJs

InjectJsは、 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が出力に表示されます。 また、ファイルinject.jsが正常に追加されたため、addfile変数についてもtrueを示しています。