访问量: 10 次浏览
PhantomJS是一个可以在无需浏览器的情况下执行JavaScript的平台。为了实现这一点,使用了以下方法,用于添加Cookie、删除Cookie、清除Cookie、退出脚本、注入JS等。
我们将在本章节中详细讨论这些PhantomJS方法及其语法。类似的方法,如 addcookie 、injectjs 存在于 webpage 模块中,将在后续章节中讨论。
PhantomJS公开了以下方法,可以帮助我们在无浏览器的情况下执行JavaScript:
现在让我们通过示例详细了解这些方法。
addCookie 方法用于添加Cookie并存储在数据中。它类似于浏览器如何存储Cookie。它接受一个参数,该参数是一个具有所有Cookie属性的对象,语法如下所示:
它的语法如下:
phantom.addCookie ({
"name" : "cookie_name",
"value" : "cookie_value",
"domain" : "localhost"
});
name、value和domain是要添加到addcookie函数的必需属性。如果cookie对象中缺少其中任何一个属性,该方法将失败。
以下是 addcookie 方法的示例。
var page = require('webpage').create(),url = 'http://localhost/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
代码注释是自解释的。
该方法允许删除所有的 cookie 。
其语法如下所示−
phantom.clearCookies();
这个概念的工作方式类似于在浏览器菜单中选择删除浏览器 cookies 。
下面是一个 clearCookies 方法的示例。
var page = require('webpage').create(),url = 'http://localhost/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
删除CookieJar中’name’属性与cookieName匹配的任何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({ //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
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用于在phantom中添加附加js文件。如果在当前目录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 文件成功添加。