所以,我有这段代码......
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();
这有效,但它只是推文:“撰写新推文” 好像文本永远不会改变。
所以我的问题是,我做错了什么或者这是某种错误?如果是这样,那是一种解决方法吗?提前谢谢。
答案 0 :(得分:0)
首先,使用fillSelectors()或fillXPath()方法填充表单。
并确保选择器'div#tweet-box-mini-home-profile.tweet-box.rich-editor.notie'是正确的。