javascript - CasperJS 奇怪的评估行为

标签 javascript phantomjs casperjs

所以,我有这个代码...

var config = require('./config.js');
var casper = require('casper').create(config.casper);

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

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

function run () {

    casper.start();

    casper.thenOpen('https://twitter.com', function () {
        if ( this.exists('form[action="https://twitter.com/sessions"].signin') ) {
            this.echo('logging in...');
            this.evaluate(function (username, password) {
                document.querySelector('#signin-email').value = username;
                document.querySelector('#signin-password').value = password;
                document.querySelector('.flex-table-btn').click();
            }, config.users.user.twitter.username, config.users.user.twitter.password); 
        } else {
            this.echo('Alreaddy logged in, proceed...');
        }
    });

    casper.waitForSelector('#tweet-box-mini-home-profile', function () {
        if ( this.exists('#tweet-box-mini-home-profile') ) {
            this.evaluate(function (text) {
                document.querySelector('div#tweet-box-mini-home-profile.tweet-box.rich-editor.notie').innerText = text;
                document.querySelector('button.btn.primary-btn.tweet-action.js-tweet-btn').click();
            }, 'Test using automation');
            this.wait(10000, function () {
                this.echo ('Finished waiting, closing app now');
            }); //wait for 10 seconds before closing
        } else {
            this.echo('Failed to logging in');
        }
    });

    casper.run();

}

run();

登录部分工作正常,我可以登录到仪表板。

但是第二步,推文步骤抛出错误

Page Error: TypeError: 'null' is not an object (evaluating 'document.querySelector('div#tweet-box-mini-home-profile.tweet-box.rich-editor.notie').innerText = text')

并且该推文不会被发布。我已经在浏览器上手动尝试过,效果很好。

var config = require('./config.js');
var casper = require('casper').create(config.casper);

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

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


function run () {

    casper.start();

    casper.thenOpen('https://twitter.com', function () {
        if ( this.exists('form[action="https://twitter.com/sessions"].signin') ) {
            this.echo('logging in...');
            this.evaluate(function (username, password) {
                document.querySelector('#signin-email').value = username;
                document.querySelector('#signin-password').value = password;
                document.querySelector('.flex-table-btn').click();
            }, config.users.dida.twitter.username, config.users.dida.twitter.password); 
        } else {
            this.echo('Alreaddy logged in, proceed...');
        }
    });

    casper.waitForSelector('#tweet-box-mini-home-profile', function () {
        if ( this.exists('#tweet-box-mini-home-profile') ) {
            this.evaluate(function (text) {
                console.log('=========== Putting text ============');
                document.querySelector('#tweet-box-mini-home-profile > div').innerHTML = text;
            }, 'Test using automation');
        } else {
            this.echo('Failed to logging in');
        }
    });

    casper.then(function () {
        if ( this.exists('.js-tweet-btn') ) {
            //recheck the tweet
            this.echo(this.getHTML('#tweet-box-mini-home-profile'));
            this.echo('============= Clicking Submit Button ==============');
            this.click('.js-tweet-btn');
            this.wait(10000, function () {
                this.echo ('Finished waiting, closing app now');
            }); //wait for 10 seconds before closing
        } else {
            this.echo('Submit button not found');
        }
    })

    casper.run();

}

run();

这有效,但它只能发推文:“撰写新推文” 就好像文本永远不会改变一样。

所以我的问题是,我做错了什么还是这是某种错误?如果是这样,这是一个解决方法吗?预先感谢您。

最佳答案

首先,使用 fillSelectors() 或 fillXPath() 方法来填写表单。

并确保选择器“div#tweet-box-mini-home-profile.tweet-box.rich-editor.notie”正确。

关于javascript - CasperJS 奇怪的评估行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20590387/

相关文章:

javascript - 使用 jQuery 删除类时,将该类包含在选择器中是否更好?

javascript - 为什么 [] == ![] 在 JavaScript 中为真?

javascript - 将提交按钮与评论框的 Enter 按键集成

html - Python + Selenium + PhantomJS 呈现为 PDF - 多个文件

java - selenium 和 phantomjs 连接重置

javascript - 当抓取 Google 搜索时更改设置特定用户代理字符串时,CasperJS 返回不同的结果

javascript - 如何根据日期输入将 12 个字符串之一传递到模态?

java - 与最新版本 Selenium Webdriver 和 PhantomJs 的兼容性问题

javascript - CasperJS 无法填充谷歌搜索字段

javascript - 如何单击 "select option"然后使用 casperjs 评估加载的内容