提交表单或点击提交输入无效

Submit form or click submit input not working

我目前正在使用 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]", "xxxxxxx@gmail.com", {
        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();

截图

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>

我相信以下内容应该可以解决您的问题:

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"]': 'xxxxxx@gmail.com',
        '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/

以上代码运行后的截图为:

信息'no account found for this email'在我们登录后出现。所以,在这种情况下,登录点击实际上是有效的。

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

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

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