如何在Casperjs中通过输入id填写表格
How to fill a form by id of input in Casperjs?
我正在使用Casperjs 1.1.0-beta3,并试图通过"id"选择器填写表单。我已经成功地使用了"input[name='userID']",但使用"id"作为选择器总是失败,并出现类似以下的错误。
CasperError:填写表单时遇到错误:表单中没有与css选择器"#header my account userid"匹配的字段;没有与表单中的css选择器"#header我的帐户密码"匹配的字段
方法1效果良好。方法2、3、4均失败。我一次只尝试一种方法,并对其他方法进行评论。我还为这个问题去掉了多余的表格标签。
我发现这个关于同一主题的stackoverflow问题仍然不起作用。
有什么想法吗?
HTML
<input id="header-my-account-userid" name="userID" class="m-my-account-userid" maxlength="80" autocomplete="off" placeholder="Email or Rewards #" type="text">
<input id="header-my-account-password" name="password" class="m-my-account-password" maxlength="20" autocomplete="off" placeholder="Password" type="password">
<button type="submit" name="submit" id="header-my-account-sign-in" class="analytics-click m-button-default" title="Sign In">Sign In</button>
Casperjs脚本
casper.then(function() {
casper.waitForSelector('form[name=directLoginForm]', function() {
// Method 1 Works
this.fillSelectors("form[name=directLoginForm]", {
'input[name=userID]' : username,
'input[name=password]' : password
}, true);
// Method 2 Does not work
this.fillSelectors("form[name=directLoginForm]", {
'input[id="header-my-account-userid"]' : username,
'input[id="header-my-account-password"]' : password
}, true);
// Method 3 Does not work
this.fillSelectors("form[name=directLoginForm]", {
'header-my-account-userid' : username,
'header-my-account-password' : password
}, true);
// Method 4 Does not work
this.fillSelectors("form[name=directLoginForm]", {
'#header-my-account-userid' : username,
'#header-my-account-password' : password
}, true);
});
});
我试过了,它们都能工作(除了3,因为你的选择器无效)。我将bool设置为false以查看更改,但您不应该有"填写表单时遇到错误:没有与css选择器"#header my account userid"匹配的字段"。
这不是一个casper错误,所以,它是特定于您的情况。由于不明原因,它无法识别您的id。
this.sendKeys('input[id="header-my-account-userid"]', username);
sendKeys也可以。
等待加载表单以使用选择器填充表单。使用除waitForResource()之外的have waitForSelector()、waitFor()、wait()等
casper.start('your_url_here',function(){
this.echo(this.getTitle());
});
casper.waitForResource("your_url_here",function() {
this.fillSelectors('#loginform', {
'input[name="Email"]' : 'your_email',
'input[name="Passwd"]': 'your_password'
}, true);
});
一个最小的例子表明,只有方法3不应该工作。我怀疑你通过检查下一页是否加载来检查"工作"表单。可能是因为casper找不到提交按钮。尝试明确地单击按钮。
此外,PhantomJS有时会遇到未引用属性选择器的问题。您应该更改
"form[name=directLoginForm]"
至
"form[name='directLoginForm']"
我发现当casperJS对浏览器来说太快时就会发生这种情况,而casper.wait()
将解决这个问题。
这是一个例子:
// Make a test order
casper.then(function() {
casper.waitForSelector('#onestepcheckout-form', function() {
this.fillSelectors('#onestepcheckout-form', {
'input[id="billing:telephone"]': '12344534',
'input[id="billing:postcode"]': '432323',
'input[id="billing:city"]': 'London',
'input[id="billing:street1"]': 'Lambada is a good music'
}, false);
})
casper.wait(5000, function() {
this.fillSelectors('#onestepcheckout-form', {
'#sagepaydirectpro_cc_owner': 'Fizipit o Moyazoci',
'input[id="agreement-1"]': true
}, true);
})
this.test.pass('form populated');
});
- 如何将输入类型值或id从一个jsp传递到另一个jsp页面
- 禁用Tab键以进行具有特定Div ID的输入
- Javascript获取具有不同id的文件数's来自没有多个属性的文件输入元素
- <输入id=“;“到期”;type=“;月份;min=“;2016-05”>如何填写“;min”;属性与当前
- 更改具有相同id的输入类型的所有值
- 如何获取具有相同id的多个输入值
- 使用getElementById设置动态ID的输入字段的样式
- 通过单击链接(兄弟姐妹?)从无线电输入中获取id
- 传递来自<输入类型=“;文件“;id=“;文件“/>尽管还有一个按钮点击
- 动态添加id's到输入字段
- 为什么Bootstrap typeahead不适用于具有相同ID的输入元素
- 当一个输入与另一个输入的 ID 具有相同的名称时的含义
- jquery如何检测当前输入id
- 使用AJAX发送隐藏的ID输入字段
- 验证表单按id输入val
- 按钮更改由按钮id输入的表单文本区域
- 如何使用 id 输入类型=“文本”JavaScript 中的值设置自动 2 个十进制数
- 设置 ID 输入 (DOM) 问题
- 使用值动态更改输入id-输入类型=范围
- 选择与实习生在CSS选择器ID输入字段