在Casperjs中使用url的问题

时间:2013-11-23 09:54:54

标签: javascript phantomjs casperjs

我的网站网址存在问题,我正在使用casperjs。我希望获得标题并测试此页面中的类或ID,并将值设置为此网站中的文本框通过使用casperjs。

//var url = 'https://www.google.com.kh/';
//this url below it is not work with my code if the url above it work normal
var url = 'https://teleservices.paris.fr/etatcivil/jsp/site/RunStandaloneApp.jsp?page=formengine&form=naissance';

var casper = require('casper').create();
    casper.start(url, function() {
            this.echo(url);

            this.echo("start page...");

    });
    casper.then(function(){
        this.wait(1000,function(){
            this.echo('Page: ' + this.getTitle());
            });

    }),
    casper.then(function() {
             if(this.exists("div.job-search-box-text")){
                    this.echo("this is id found");
                }
               else{
                    this.echo("no ID found");
                }

    }),
    casper.then(function(){
        this.wait(1000,function(){
            this.fillSelectors('form', {

                    'input[name="title"]' : "12"
            });
            this.capture('result.png');
            });

});


casper.run();

设置var url = 'https://teleservices.paris.fr/etatcivil/jsp/site/RunStandaloneApp.jsp?page=formengine&form=naissance'; my result code can not display the title of the web page,can not get the each id or class in this webpage,can not capture....I'm stuck with this anyone have solution , I have tried it for long time,please help me, thanks.

时,我遇到了此网址的问题

2 个答案:

答案 0 :(得分:1)

哇 - 我无法弄清楚这是怎么回事。我得到了代码工作,但我无法弄清楚究竟修复了什么。

该网站有一些奇怪的地方,有时我的图片会有404,我注意到一个奇怪的网址结构。注意// css /

https://teleservices.paris.fr/etatcivil//css/plugins/images/local/skin/plugins/etatcivil/left-first-srub.gif

我希望有一些重要的教训可以帮助你 - 我确信有,但我不知道:)

var url = "https://teleservices.paris.fr/etatcivil/jsp/site/RunStandaloneApp.jsp?page=formengine&form=naissance";

var casper = require('casper').create({
    logLevel:"debug",
    verbose: true,
    waitTimeout: 6000
});



casper.userAgent('Mozilla/4.0 (compatible; MSIE 10.0; Windows NT 6.1)');

casper.start(url, function() {
    this.echo(url);
    this.echo("start page...");
});

casper.waitForSelector('#formengine-menu', function() {
    this.echo('Page: ' + this.getTitle());

});

casper.then(function() {
    if(this.exists("#naissancelieuEtDate")){    this.echo("Form naissancelieuEtDate exists");   }
    else{   this.echo("no ID found");   }
});

casper.then(function(){

    this.fill('#naissancelieuEtDate', {
            'dateActeJour' : "12"
    });
    this.capture('result.png');

 });

casper.run();

答案 1 :(得分:0)

我在OSX mavericks上使用casperjs 1.0.2,我没有问题来显示标题。 您检查div.job-search-box-text是否存在但在两种情况下不存在。 而fillSelectors代码输出错误:没有名称标题的输入字段。

这是网址https://www.google.com.kh/

的输出
https://www.google.com.kh/
start page...
Page: Google
no ID found
TypeError: 'undefined' is not a function (evaluating 'this.fillSelectors('form',     {'input[name="title"]' : "12"})')
/Users/jeff/Downloads/code/automation/casperjs-1.0.3/scripts/test.js:24
/Users/jeff/Downloads/code/automation/casperjs-1.0.3:1677 in _check

这是网址https://www.google.com.kh/

的输出
https://teleservices.paris.fr/etatcivil/jsp/site/RunStandaloneApp.jsp?page=formengine&form=naissance
start page...
Page: Acte de naissance
no ID found
TypeError: 'undefined' is not a function (evaluating 'this.fillSelectors('form', {'input[name="title"]' : "12"})')
/Users/jeff/Downloads/code/automation/casperjs-1.0.3/scripts/test.js:24
/Users/jeff/Downloads/code/automation/casperjs-1.0.3:1677 in _check