【发布时间】:2014-04-22 17:40:23
【问题描述】:
我正在使用 Casperjs 1.1.0-beta3 并尝试通过“id”选择器填写表单。我已成功使用“input[name='userID']”,但使用“id”作为选择器总是会失败,并出现类似于以下的错误。
CasperError:填写表单时遇到错误:表单中没有匹配css选择器“#header-my-account-userid”的字段;表单中没有匹配 css 选择器“#header-my-account-password”的字段
方法 1 工作正常。方法2、3、4都失败了。我一次只尝试一种方法并评论其他方法。我还为这个问题剪掉了多余的表单标签。
我在同一主题上发现了这个 stackoverflow question,但它仍然不起作用。
有什么想法吗?
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);
});
});
【问题讨论】:
-
嗯,这很奇怪,应该可以,试试 this.sendKeys('input[id="header-my-account-userid"]', username);
-
感谢您的回复。我认为它也会起作用。我已经看到了方法#2 的几个示例,据说它适用于人们。我唯一牵强的想法是,就我而言,“名称”和“身份”是不同的。加上 '-' 在 id 本身。我不敢相信这会是问题,但我以前见过奇怪的东西。我会尝试你的想法,然后再回来,因为它只是当地时间上午 4:30 :)
-
@Fanch,我试过你的代码 sn-p。 Pycharm 将其读取为无效,并且在我使用它时脚本挂起。谢谢你的尝试。 :)
-
附带说明,我了解到您可以通过 phantomjs 运行 casperjs 脚本,只需稍加修改即可。我能够使用我的方法#1,它运行良好。但是,使用方法 #2 并通过 phantomjs 运行会使它崩溃。错误??????
标签: javascript phantomjs casperjs