[译文]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('http://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('http://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('http://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('http://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('http://domain.tld/page.html', function() {
this.test.assertExists(x('//*[@id="plop"]'), 'the element exists');
});
警告:1、当你使用casperjs.fill()去填充fields时,xpath存在使用限制2、PhantomJS只支持css3选择器使用uploadFile method
[译文]casperjs使用说明-选择器的更多相关文章
- [译文]casperjs使用说明-测试
capserjs自带了一个测试框架,它提供了一个使你能够更容易的测试你的web应用的工具集. 注意: 1.1版本变更 这个测试框架,包括它的所有API,仅能使用在casperjs test子命令下 如 ...
- [译文]casperjs使用说明-使用命令行
使用命令行 Casperjs使用内置的phantomjs命令行解析器,在cli模块里,它传递参数位置的命名选项 但是不要担心不能熟练操控CLI模块的API,一个casper实例已经包含了cli属性,允 ...
- [译文]casperjs的API-clientutils模块
casper提供了少量的客户端接口用来进行远程DOM环境注入,通过clientutils模块的ClientUtils类实例中的__utils__对象来执行: casper.evaluate(funct ...
- [译文]casperjs 的API-casper模块
Casper class: 可以通过这个模块的create()方法来获取这个模块的一个实例,这是最容易的: var casper = require('casper').create(); 我们也可以 ...
- [译文]casperjs的API-mouse模块
mouse类 这个类是对各种鼠标操作的抽象,比如移动,点击,双击,滚动等.它要求一个已经获得DOM属性的casper对象,能用这种方式创造一个鼠标对象: var casper = require(&q ...
- [译文]casperjs的API-colorizer模块
colorizer模块包含了一个Colorizer类,它能够生成一个标准化的颜色字符串: var colorizer = require('colorizer').create('Colorizer' ...
- CSS 笔记——选择器
1. 选择器 (1)类型选择器(标签选择器) 基本语法 E { sRules } 使用说明 类型选择器.以文档对象(Element)类型作为选择器. 选择面较大,适合做某种标签元素外观的常规设置. 代 ...
- jquery选择器使用说明
在jquery中选择器是一个非常重要的东西,几乎做什么都离不开它,下面我总结了jquery几种选择器的用法.以方便后面直接可以用到!! 层次选择器: 1.find()://找到该元素的子元素以及孙子元 ...
- jQuery css()选择器使用说明
css选择器只是jquery中的一个功能罢了,下面我来给各位朋友详细介绍jQuery css()选择器使用方法与说明详解,有需要了解学习的同学可参考. CSS操作有一个重要的方法:CSS() CSS( ...
随机推荐
- cdoj913-握手 【Havel定理】
http://acm.uestc.edu.cn/#/problem/show/913 握手 Time Limit: 2000/1000MS (Java/Others) Memory Limit ...
- 微信小程序(应用号)开发资源汇总整理
开源项目 wechat-weapp-gank - 微信小程序版Gank客户端 wechat-dribbble - 微信小程序-Dribbble wechatApp-demo - 微信小程序 DEMO ...
- 825. Friends Of Appropriate Ages有效的好友请求的数量
[抄题]: Some people will make friend requests. The list of their ages is given and ages[i] is the age ...
- 695. Max Area of Island最大岛屿面积
[抄题]: 求最多的联通的1的数量 Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (repre ...
- Python学习笔记_从CSV读取数据写入Excel文件中
本示例特点: 1.读取CSV,写入Excel 2.读取CSV里具体行.具体列,具体行列的值 一.系统环境 1. OS:Win10 64位英文版 2. Python 3.7 3. 使用第三方库:csv. ...
- C++ std::unordered_map
std::unordered_map template < class Key, // unordered_map::key_type class T, // unordered_map::ma ...
- [c++] polymorphism without virtual function
polymorphism without virtual function
- [SoapUI] 比较两个不同环境下XML格式的Response, 结果不同时设置Test Step的执行状态为失败
import org.custommonkey.xmlunit.* def responseTP=context.expand( '${Intraday Table_TP#Response}' ) d ...
- Spring.net ObjectWrapper对象的包装(反射机制)有点明晰方便
Company c = new Company(); List<PropertyInfo> ps = c.GetType().GetProperties().ToList(); var p ...
- 认识Web前端、Web后端、桌面app和移动app新开发模式 - 基于Node.js环境和VS Code工具
认识Web.桌面和移动app新开发模式 - 基于Node.js环境和VS Code工具 一.开发环境的搭建(基于win10) 1.安装node.js和npm 到node.js官网下载安装包(包含npm ...