脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-casperjs使用说明-选择器,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 casPErjs的选择器可以在dom下工作,他既支持css也支持xpath.
下面所有的例子都基于这段htML代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<tITle>My page</title>
</head>
<body>
<h1 class="page-title">Hello</h1>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
<footer><p>©2012 myself</p></footer>
</body>
</html>
CSS3
默认情况下,casperjs将字符串视为css3表达式去查找dom元素
如果要查找实例页面里的<h1 class="page-title">,你可以这样做:
VAR casper = require('casper').create();
casper.start('https://domain.tld/page.html', function() {
if (this.exists('h1.page-title')) {
this.echo('the heading exists');
}
});
casper.run();
或者你可以使用测试框架:
casper.test.begin('The heading exists', 1, function suite(test) {
casper.start('https://domain.tld/page.html', function() {
test.assertExists('h1.page-title');
}).run(function() {
test.done();
});
});
有一些便捷的测试方法依赖于选择器:
casper.test.begin('Page content tests', 3, function suite(test) {
casper.start('https://domain.tld/page.html', function() {
test.assertExists('h1.page-title');
test.assertSelectorHasText('h1.page-title', 'Hello');
test.assertVisible('footer');
}).run(function() {
test.done();
});
});
XPath
你也可以选择使用xpath表达式来替代css选择器:
casper.start('https://domain.tld/page.html', function() {
this.test.assertExists({
type: 'xpath',
path: '//*[@class="plop"]'
}, 'the element exists');
});
为了更容易的去使用和读xpath,selectXPath能给你提供帮助:
var x = require('casper').selectXPath;
casper.start('https://domain.tld/page.html', function() {
this.test.assertExists(x('//*[@id="plop"]'), 'the element exists');
});
觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!
以上是脚本宝典为你收集整理的javascript代码实例教程-casperjs使用说明-选择器全部内容,希望文章能够帮你解决javascript代码实例教程-casperjs使用说明-选择器所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。