PhantomJS - 方法
PhantomJS 是一个无需浏览器即可执行 JavaScript 的平台。为此,使用以下方法,这些方法有助于添加 Cookie、删除、清除、退出脚本、注入 JS 等。
我们将在本章中进一步讨论这些 PhantomJS 方法及其语法。类似的方法,即addcookie,injectjs存在于网页模块中,将在后续章节中讨论。
PhantomJS 公开了以下方法,可以帮助我们在没有浏览器的情况下执行 JavaScript −
- addCookie
- clearCookie
- deleteCookie
- Exit
- InjectJS
现在让我们通过示例详细了解这些方法。
addCookie
addcookie 方法用于添加 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 = 'http://localhost/tasks/a.html'; page.open(url, function(status) { if (status === 'success') { phantom.addCookie({ //添加名称 cookie1,其值为 1 name: 'cookie1', value: '1', domain: 'localhost' }); phantom.addCookie({ // 添加值为 2 的 cookie2 name: 'cookie2', value: '2', domain: 'localhost' }); phantom.addCookie({ // 添加值为 3 的 cookie3 name: 'cookie3', value: '3', domain: 'localhost' }); console.log('Added 3 cookies'); console.log('Total cookies :'+phantom.cookies.length); // 将输出添加到 url 的 cookie 总数。 } 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 = 'http://localhost/tasks/a.html'; page.open(url, function(status) { if (status === 'success') { phantom.addCookie({ //添加名称 cookie1,其值为 1 name: 'cookie1', value: '1', domain: 'localhost' }); phantom.addCookie({ // 添加值为 2 的 cookie2 name: 'cookie2', value: '2', domain: 'localhost' }); phantom.addCookie({ // 添加值为 3 的 cookie3 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 = 'http://localhost/tasks/a.html'; page.open(url, function(status) { if (status === 'success') { phantom.addCookie({ //添加名称 cookie1,其值为 1 name: 'cookie1', value: '1', domain: 'localhost' }); phantom.addCookie({ // 添加值为 2 的 cookie2 name: 'cookie2', value: '2', domain: 'localhost' }); phantom.addCookie({ // 添加值为 3 的 cookie3 name: 'cookie3', value: '3', domain: 'localhost' }); console.log('Added 3 cookies'); console.log('Total cookies :'+phantom.cookies.length); //将输出添加到 url 的 cookie 总数。 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'); // 输出 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 文件。如果在当前 目录 librarypath 中找不到该文件,则 phantom 属性 (phantom.libraryPath) 将用作跟踪路径的附加位置。如果文件添加成功,则返回 true,否则,如果无法找到该文件,则返回 false 以防失败。
语法
其语法如下 −
phantom.injectJs(filename);
示例
让我们看下面的例子来了解injectJs的用法。
文件名:inject.js
console.log("Added file");
文件名:addfile.js
var addfile = injectionJs(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。