2016-02-25 243 views
1

我目前正在与phantomjs 2.1.1casper 1.1.0-beta5自动执行基本登录过程。但是,我没有最好的运气。在下面,我可以成功填写带有值的表单,但是当我尝试提交时会出现问题。我曾尝试使用clickselectors等提交表单,但没有结果。有关如何用下面的代码成功登录的任何想法?提交表单或单击提交输入不工作

var casper = require('casper').create({ 
    verbose: true, 
    logLevel: 'error', 
    viewportSize: { 
     width: 1280, 
     height: 720 
    }, 
    pageSettings: { 
     ignoreSslErrors: true, 
     loadImages: false, // do not load images 
     loadPlugins: false, // do not load NPAPI plugins (Flash, Silverlight, ...) 
     webSecurityEnabled: false 
    } 
}); 

var x = require('casper').selectXPath; 
phantom.cookiesEnabled = true; 
var fs = require('fs'); 
var cookies = JSON.stringify(phantom.cookies); 
fs.write("cookies.txt", cookies, 644); 

casper.start('https://www.quora.com/login', function() { 
    this.sendKeys("input[name=email]", "[email protected]", { 
     keepFocus: true 
    }); 
    this.page.sendEvent("keypress", this.page.event.key.Tab); 
    this.sendKeys("input[name=password]", "xxxxxxx", { 
     keepFocus: true 
    }); 
}); 

casper.then(function() { 
    this.evaluate(function() { 
     document.getElementById('__w2_IXl7ObX_login_form').submit(); 
    }); 
}); 

casper.then(function() { 
    this.capture('test.png'); 
}); 

casper.run(); 

截图

enter image description here

HTML

<form id="__w2_gptxdFS_login_form" class="inline_login_form" method="POST"> 
    <div class="title">Login</div> 
    <div class="form_inputs"> 
     <div class="form_column"> 
      <input id="__w2_gptxdFS_email" class="text header_login_text_box ignore_interaction" type="text" w2cid="gptxdFS" tabindex="1" name="email" placeholder="Email" group="__w2_gptxdFS_interaction"> 
     </div> 
     <div class="form_column"> 
      <input id="__w2_gptxdFS_password" class="text header_login_text_box ignore_interaction" type="password" w2cid="gptxdFS" tabindex="2" name="password" placeholder="Password" group="__w2_gptxdFS_interaction"> 
     </div> 
     <input id="__w2_gptxdFS_submit_button" class="submit_button ignore_interaction submit_button_disabled" type="submit" w2cid="gptxdFS" tabindex="4" value="Login" group="__w2_gptxdFS_interaction"> 
    </div> 
</form> 
+1

表单中是否有任何提交按钮? –

+0

@jobinsjohn表单有一个提交输入栏,标签为'login' – MaryCoding

+0

您可以粘贴页面外观样式和表单的HTML代码截图吗?我已经给出了一个解决方案,但需要一个按钮才能出现,我认为这是最主要的情况。您将有一个用户名字段,一个密码字段和一个说明登录或登录的按钮。 – prabugp

回答

1

我相信下面的应该解决这个问题为您提供:

var casper = require('casper').create({ 
    verbose: true, 
    logLevel: 'debug', 
    viewportSize: { 
      width: 1280, 
     height: 720 
    }, 
    pageSettings: { 
     ignoreSslErrors: true, 
     loadImages: false, // do not load images 
     loadPlugins: false, // do not load NPAPI plugins (Flash, Silverlight, ...) 
     webSecurityEnabled: false, 
     localToRemoteUrlAccessEnabled: false 
    } 
}); 

casper.on('remote.message', function(msg) { 
    this.echo('remote message caught: ' + msg); 
}); 

casper.on('page.error', function(msg, trace) { 
    this.echo('Error: ' + msg, 'ERROR'); 
}); 

casper.start('https://www.quora.com', function() { 
    this.echo ('loggin in'); 
    this.fillSelectors('div.form_inputs', { 
     'input[type="text"]': '[email protected]', 
     'input[type="password"]': 'xxxxxxx' 
    }, false); 
}); 

casper.then(function() { 
    this.click('input.submit_button.ignore_interaction'); 
}).wait(5000, function(){}); 

casper.then(function() { 
    this.capture('test.png'); 
}); 

casper.run(); 

我跑这与casperjs --ignore-ssl-errors=true --ssl-protocol=any test.js 否则,我在调试模式下运行脚本时看到[warning] [phantom] Loading resource failed with status=fail (HTTP 500): https://www.quora.com/

上面的代码后的截图运行是:

Error message after clicking login

消息“找不到与此电子邮件帐户”发生后,我们登录。所以,在这种情况下,登录点击实际上正在工作。

在代码中,我等待5秒钟完成登录调用,但是您可以等待登录后发生的页面中的某个html元素。

+0

只需要将SPA中的所有屏幕转换为PPT的人。我可以问'为什么这么好奇'吗? – prabugp

+0

啊。 “我们”在我们的团队中与OP没有任何关系。 – prabugp

+0

这确实工作完美。根据stackoverflow我不能奖赏赏金,直到时间到了。 – MaryCoding

1

发送\n(输入密码)在您的密码,它应填写后立即提交表格。

this.sendKeys("input[name=password]", "xxxxxxx\n", { 
     keepFocus: true 
    });