【问题标题】:form submit with casperjs - new url doesn't load使用 casperjs 提交表单 - 未加载新网址
【发布时间】:2017-07-04 13:35:44
【问题描述】:

目标

在这里,我尝试在 Pubmed website。假设我正在搜索的术语是Hello。最后我期望的是在点击搜索按钮后登陆结果页面。

问题

提交代码在 chrome 浏览器的 javascript 控制台上完美运行。但它不适用于 casperjs。 current-url 似乎保持不变。我不知道问题出在哪里。

我的代码

// USAGE: casperjs test navigation_test.js

var config = {
  url: 'https://www.ncbi.nlm.nih.gov/pubmed/',
};

config.form = {
  "term": "Hello",
};

casper.test.begin('Testing navigation and forms', 2, function suite(test) {
  test.comment('⌚  Loading ' + config.url + '...');


  casper.start(config.url, function() {
    // adjust the view port
    this.viewport(1280, 1024);
  });

  // #1 method-2 (short)
  casper.then(function() {
   this.fill('form#EntrezForm', config.form, true);   
  })

  // #2
  casper.then(function() {
    test.assertUrlMatch(/term/, 'New location is ' + this.getCurrentUrl());
  });

  casper.run(function () {
    test.done();
  });

});

额外代码

上述代码中的#1 method 很短。我还尝试了更长的版本,如下所示。但它也没有工作。

//  #1 method-1 (long)
casper.then(function() {
  this.evaluate(function() {
      $('term').value = "Hello"
  });

  test.assertEvalEquals(function () {
      return $('term').value;
  }, "Hello", 'The search was filled out properly.');  

  this.click('button[id="search"][type="submit"][class="button_search nowrap"]');
  // OR
  // this.clickLabel('Search', 'button');
  // OR 
  /*this.evaluate(function() {
    $('search').click();
    // OR
    // document.getElementById('search').click();
  });*/

});

【问题讨论】:

标签: javascript node.js phantomjs casperjs


【解决方案1】:

在我提交表单或请求新的 uri 后,我 ALWAYS 使用 waitFor 之一作为选择器。我有时会注意到仅使用 .then 有时会失败。

这很好(我还注意到你的 url 匹配有点错误)

var config = {
    url: 'https://www.ncbi.nlm.nih.gov/pubmed/',
};

config.form = {
  "term": "Hello",
};

casper.test.begin('Testing navigation and forms', 2, function suite(test) {

    casper.start(config.url, function () {
        this.viewport(1280, 1024);
    });

    casper.then(function () {
        test.assertTitle("Home - PubMed - NCBI", "Title is Home - PubMed - NCBI");
        this.fill('form#EntrezForm', config.form, true);
    });

    //Changes made here!
    casper.waitForText("Search results", function () {
        test.assertUrlMatch(/pubmed\/\?term=Hello/, 
            'New location is ' + this.getCurrentUrl());
    });

    casper.run(function () {
        test.done();
    });

});

更新试试这个

添加失败条件并抓取屏幕以查看发生了什么(或未发生什么)

casper.waitForText("Search results", function () {
  test.assertUrlMatch(/pubmed\/\?term=Hello/, 
  'New location is ' + this.getCurrentUrl());
}, function() {
  casper.capture("grab.png");
});

您还可以在casper.test 行的正下方添加一个用户代理字符串

casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X)');    

您也可以尝试增加超时时间,使其超过 5 秒,例如

    casper.waitForText("Search results", function () {
        test.assertUrlMatch(/pubmed\/\?term=Hello/, 
            'New location is ' + this.getCurrentUrl());
    }, null, 10000);

更新试试这个 - 远射!

尝试用这个 coede 替换。应该是填表的同时提交...

casper.then(function () {
  test.assertTitle("Home - PubMed - NCBI", "Title is Home - PubMed - NCBI");
  this.fillSelectors('form', {
    "input[id='term']": "Hello"
  }, true);
});

【讨论】:

  • 令人惊讶的是,您的上述代码对我不起作用...我收到以下错误。 error screenshot
  • 我在Ubuntu 16.04.2 上使用casperjs 1.1.4phantomjs 2.1.1nodejs v4.2.6。据我所知,我认为执行“搜索”按钮存在问题。这是grab.png 的图片。在这部分代码中,页面应该已经登陆“搜索结果”,但事实并非如此。这是现在current error 的样子。
  • 发布了另一个更新。一切都在 Windows 上运行良好 :)
  • 上面的代码似乎与Ubuntu 存在问题。可能是我的安装有问题。但是在你暗示了Windows 之后,我试了一下。显然,您的解决方案在 Windows 机器上运行良好。向我暗示Windows 是一个不错的方向:) 谢谢!您可以将此线程标记为已解决:)
猜你喜欢
  • 1970-01-01
  • 2015-11-20
  • 1970-01-01
  • 2015-03-31
  • 2018-07-31
  • 2017-06-22
  • 2013-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多