【问题标题】:How to fill a form by id of input in Casperjs?如何通过 Casperjs 中的输入 id 填写表单?
【发布时间】: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


【解决方案1】:

我试过了,它们都可以工作(除了 3,因为您的选择器无效)。 我将 bool 设置为 false 以查看更改,但您不应出现“填写表单时遇到的错误:没有匹配 css 选择器“#header-my-account-userid”的字段。

这不是 casper 错误,所以它是针对您的情况的。由于不明原因,它无法识别您的 ID。

this.sendKeys('input[id="header-my-account-userid"]', username);

sendKeys 也可以。

【讨论】:

    【解决方案2】:

    等待表单被加载以使用选择器填充表单。使用除了 waitForResource() 之外的 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】:

      一个最小的例子表明只有方法 3 不应该工作。我怀疑您通过检查下一页是否加载来检查“工作”表单。 casper 可能没有找到提交按钮。尝试显式单击该按钮。

      此外,PhantomJS 有时会出现不带引号的属性选择器的问题。你应该改变

      "form[name=directLoginForm]"
      

      "form[name='directLoginForm']"
      

      【讨论】:

      • 找到按钮没问题。错误是关于使用某些选择器查找“输入”。查看我的问题中的错误。
      【解决方案4】:

      我发现当 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');
          });
      

      【讨论】:

      • 没错,但我认为在我做任何事情之前使用'casper.waitForSelector'总是更好。这样,如果网站花费的时间甚至超过 5000,您就应该被覆盖。
      • @fatfantasma 确实如此,如果它看起来快于 5 秒,您不必等待更长时间
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      • 2020-12-14
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多