casperjs中fill()方法的使用
2015-10-16 22:23
453 查看
Signature: fill(String selector, Object values[, Boolean submit])
Fills the fields of a form with given values and optionally submits it. Fields are referenced by their name attribute.
Changed in version 1.1: To use CSS3 or XPath selectors instead, check the fillSelectors() and fillXPath() methods.
Example with this sample html form:
<form action="/contact" id="contact-form" enctype="multipart/form-data">
<input type="text" name="subject"/>
<textearea name="content"></textearea>
<input type="radio" name="civility" value="Mr"/> Mr
<input type="radio" name="civility" value="Mrs"/> Mrs
<input type="text" name="name"/>
<input type="email" name="email"/>
<input type="file" name="attachment"/>
<input type="checkbox" name="cc"/> Receive a copy
<input type="submit"/>
</form>
A script to fill and submit this form:
casper.start('http://some.tld/contact.form', function() {
this.fill('form#contact-form', {
'subject': 'I am watching you',
'content': 'So be careful.',
'civility': 'Mr',
'name': 'Chuck Norris',
'email': 'chuck@norris.com',
'cc': true,
'attachment': '/Users/chuck/roundhousekick.doc'
}, true);
});
casper.then(function() {
this.evaluateOrDie(function() {
return /message sent/.test(document.body.innerText);
}, 'sending message failed');
});
casper.run(function() {
this.echo('message sent').exit();
});
The fill() method supports single selects in the same way as text input. For multiple selects, supply an array of
values to match against:
<form action="/contact" id="contact-form" enctype="multipart/form-data">
<select multiple name="category">
<option value="0">Friends</option>
<option value="1">Family</option>
<option value="2">Acquitances</option>
<option value="3">Colleagues</option>
</select>
</form>
A script to select multiple options for category in this form:
casper.then(function() {
this.fill('form#contact-form', {
'categories': ['0', '1'] // Friends and Family
});
});
Warning:
1. The fill() method currently can’t fill file fields using XPath selectors; PhantomJS natively only allows
the use of CSS3 selectors in its uploadFile() method, hence this limitation.
2. Please Don’t use CasperJS nor PhantomJS to send spam, or I’ll be calling the Chuck. More seriously, please
just don’t.
Fills the fields of a form with given values and optionally submits it. Fields are referenced by their name attribute.
Changed in version 1.1: To use CSS3 or XPath selectors instead, check the fillSelectors() and fillXPath() methods.
Example with this sample html form:
<form action="/contact" id="contact-form" enctype="multipart/form-data">
<input type="text" name="subject"/>
<textearea name="content"></textearea>
<input type="radio" name="civility" value="Mr"/> Mr
<input type="radio" name="civility" value="Mrs"/> Mrs
<input type="text" name="name"/>
<input type="email" name="email"/>
<input type="file" name="attachment"/>
<input type="checkbox" name="cc"/> Receive a copy
<input type="submit"/>
</form>
A script to fill and submit this form:
casper.start('http://some.tld/contact.form', function() {
this.fill('form#contact-form', {
'subject': 'I am watching you',
'content': 'So be careful.',
'civility': 'Mr',
'name': 'Chuck Norris',
'email': 'chuck@norris.com',
'cc': true,
'attachment': '/Users/chuck/roundhousekick.doc'
}, true);
});
casper.then(function() {
this.evaluateOrDie(function() {
return /message sent/.test(document.body.innerText);
}, 'sending message failed');
});
casper.run(function() {
this.echo('message sent').exit();
});
The fill() method supports single selects in the same way as text input. For multiple selects, supply an array of
values to match against:
<form action="/contact" id="contact-form" enctype="multipart/form-data">
<select multiple name="category">
<option value="0">Friends</option>
<option value="1">Family</option>
<option value="2">Acquitances</option>
<option value="3">Colleagues</option>
</select>
</form>
A script to select multiple options for category in this form:
casper.then(function() {
this.fill('form#contact-form', {
'categories': ['0', '1'] // Friends and Family
});
});
Warning:
1. The fill() method currently can’t fill file fields using XPath selectors; PhantomJS natively only allows
the use of CSS3 selectors in its uploadFile() method, hence this limitation.
2. Please Don’t use CasperJS nor PhantomJS to send spam, or I’ll be calling the Chuck. More seriously, please
just don’t.
相关文章推荐
- casperjs中的open()和thenOpen()使用说明
- win10 IIS发布asp.net MVC网站
- 在ASP.NET MVC3项目中,自定义404错误页面
- casperjs中start方法的使用方法总结
- casperjs中Selectors使用总结
- ASP.NET MVC View向Controller传值方式总结
- ASP.NET Web API获取Model元数据
- 使用 CasperJS 进行简单的 UI 测试
- ASP.NET获取真正的客户端IP地址的6种方法
- ASP.NET Web大文件下载的实现思路及代码
- ASP.NET JSON的序列化和反序列化 之 Newtonsoft.Json
- metasploit下载地址
- 设计模式之工厂模式与反射
- asp.net OnInit、OnLoad、Page_Load、Page_Init父子页面执行顺序探究
- JasperReports+iReport在eclipse中的使用
- JasperReports+iReport打印为excel表头重复问题解决
- ASP.NET MVC Controller向View传值方式总结
- UnionPay,ChinaPay 最新 银联支付接口C#\Asp.net\MVC 版本
- ASP.NET HTMLHELPER
- Java入门到精通——调错篇之Spring2.5利用aspect实现AOP时报错: error at ::0 can't find referenced pointcut XXX 分类: Java从