2014-01-25 80 views
1

我使用此代码使用casperJS登录LinkedIn,但它不工作,登录后的标题应该是“Welcome!LinkedIn”,但它的返回“世界上最大的专业网络| LinkedIn”和文件。位置应该与网站网址不同,但是它在执行后会返回网站网址。CasperJS登录LinkedIn

var casper = require('casper').create({ 
verbose: true, 
logLevel: 'debug', 
pageSettings: { 
    loadImages: false,   // The WebPage instance used by Casper will 
    loadPlugins: false,   // use these settings 
    userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4' 
} 
}); 

// print out all the messages in the headless browser context 
casper.on('remote.message', function(msg) { 
    this.echo('remote message caught: ' + msg); 
}); 

// print out all the messages in the headless browser context 
casper.on("page.error", function(msg, trace) { 
    this.echo("Page Error: " + msg, "ERROR"); 
}); 

var url = 'https://www.linkedin.com/'; 

casper.start(url, function() { 
    // search for 'casperjs' from google form 
    console.log("page loaded"); 
    this.test.assertExists('form#login', 'form is found'); 
    this.fill('form#login', { 
     session_key: '[email protected]', 
     session_password: 'password' 
    }, true); 
}); 

casper.thenEvaluate(function(){ 
    console.log("Page Title " + document.title); 
    console.log("Your name is " + document.location); 
}); 

casper.run(); 

我在做什么错在这段代码?

回答

2

我认为脚本太快评估代码。

,所以我会用这个句子替换casper.thenEvaluate(如果您正在使用1.1卡斯帕运行脚本):

casper.waitForUrl(/nhome/, function() { 
     this.evaluate(function() { 
      console.log("Page Title " + document.title); 
      console.log("Your name is " + document.location); 
     }); 
}); 

此外,您还可以等待尝试

casper.wait(2000, function() { 
     this.evaluate(function() { 
      console.log("Page Title " + document.title); 
      console.log("Your name is " + document.location); 
     }); 
}); 

更多关于此功能的信息: http://docs.casperjs.org/en/latest/modules/casper.html#waitforurl

+0

你测试了你发布的代码吗?我得到这个错误'[error] [phantom]等待超时5000ms过期,退出。 超过5000ms的等待超时退出。 ' – scottydelta

+0

是的,它的工作原理,但我是法国人,所以也许LinkedIn不会为我提供相同的页面URL。尝试等待(2000,function(){}); –

+0

有没有新闻?谢谢。 –

0

在我的情况下,我刚刚存档了这个,当我做了这样的事情:

this.evaluate(function() { 
    $('#login-email').val('[email protected]'); 
    $('#login-email').focus(); 
}); 

this.page.sendEvent('keypress', this.page.event.key.M, null, null, 0x02000000); 
this.capture('p1.1.png'); 

this.evaluate(function() { 
    $('#login-password').val('PASSWORD'); 
    $('#login-password').focus(); 
}); 

//change to the last character of your password. 
this.page.sendEvent('keypress', this.page.event.key[3]); 

this.page.sendEvent('keypress', this.page.event.key.Enter); 
this.page.sendEvent('keypress', this.page.event.key.Enter); 

我相信他们不启用提交,直到您不输入文本框中的内容。