Twitter automation bot trigger click event

116 views
Skip to first unread message

Mate Marschalko

unread,
Sep 24, 2015, 8:21:35 AM9/24/15
to phantomjs

Hi,

I'm trying to build an app that automates liking stuff on Twitter. Pretty exciting stuff. I now it's Casper JS but the bug is most likely with Phantom JS.
The below code simply tries to login then open a Tweet on the single tweet page view then click on the favourite button.

So I'm running this piece of code with: casperjs --ignore-ssl-errors=yes --ssl-protocol=tlsv1 twitter.js

I mange to log in, the page loads and everything appears correctly on the screenshot.

The exist() function also finds the button and inside the condition the getElementInfo() function outputs the content of the button correctly. So the button is definitely loaded into the DOM.

However I can't seem to trigger a click on it and I tried everything. I'm definitely selecting the right elements as I can click the button from the Chrome dev console by simply running $('selector').click();

I appreciate your help!

var casper = require('casper').create({
    verbose: true,
    // logLevel: 'debug',
    pageSettings: {
     userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36'
    },
waitTimeout: 10000,
pageSettings: {
loadImages:  true,
loadPlugins: true
},
viewportSize: {
width: 1280,
height: 10000
}
});


var username = "yo...@email.com";
var password = "pass";

casper.start(url, function() {
    this.echo("Loading " + url);
});

casper.then(function() {
this.waitForSelector('.t1-form.signin, .js-password-field, .js-username-field',
    function pass() {

     this.echo("*** PASS: Form loaded");

        // Fill out form
this.fillSelectors('.t1-form.signin', {
    "#signin-email": username,
    "#signin-password": password
}, true);

// Click login
this.click('.submit.btn');
this.echo("Submitted login...");
    },
    function fail() {
     this.capture("failed1.png");
        this.log("Page FAILED to load", "error");
    }
);
});

function likeTopTweets(self){
       // Just loading a random tweet to test the script with
self.waitForSelector('.ProfileTweet-actionButton.js-actionButton.js-actionFavorite',
function pass() {

self.echo("*** PASS: Loaded page");

this.capture("tweet.png");

self.wait(2000, function(){
if (this.exists('.ProfileTweet-actionButton.js-actionButton.js-actionFavorite')) {
self.echo("Button found! Content:");
self.echo(self.getElementInfo('.ProfileTweet-actionButton.js-actionButton.js-actionFavorite').html);

// I get here and the content of the button is definitely there, and everything appears correctly on the screenshot

// ASSERT , I'm trying stuff here desperately, but nothing seem to work::
var hey = self.evaluate(function() {
var results = ["Hello!"];

results.push( $('.ProfileTweet-actionButton.js-actionButton.js-actionFavorite').html() );

$('.ProfileTweet-actionButton.js-actionButton.js-actionFavorite').click();
results.push( ($( $('.tweet')[0] ).hasClass("favorited") ? "FAVED!!" : "NOT :(") );

$('.ProfileTweet-actionButton.js-actionButton.js-actionFavorite').trigger("click");
results.push( ($( $('.tweet')[0] ).hasClass("favorited") ? "FAVED!!" : "NOT :(") );

$('.ProfileTweet-actionButton.js-actionButton.js-actionFavorite .IconContainer.js-tooltip').click();
results.push( ($( $('.tweet')[0] ).hasClass("favorited") ? "FAVED!!" : "NOT :(") );

$('.ProfileTweet-actionButton.js-actionButton.js-actionFavorite .IconContainer.js-tooltip').trigger("click");
results.push( ($( $('.tweet')[0] ).hasClass("favorited") ? "FAVED!!" : "NOT :(") );

$('.ProfileTweet-actionButton.js-actionButton.js-actionFavorite .IconContainer.js-tooltip .Icon--favorite').click();
results.push( ($( $('.tweet')[0] ).hasClass("favorited") ? "FAVED!!" : "NOT :(") );

$('.ProfileTweet-actionButton.js-actionButton.js-actionFavorite .IconContainer.js-tooltip .Icon--favorite').trigger("click");
results.push( ($( $('.tweet')[0] ).hasClass("favorited") ? "FAVED!!" : "NOT :(") );

return results;
});
self.echo(hey);

} else {
this.echo('Didnt exist :(');
}

// self.evaluate(function(){
//  $('.js-stream-item.stream-item .ProfileTweet-actionButton.js-actionFavorite').each(function(){
//  $(this).click();
//  });
// });
});
    },
    function fail() {
        self.log("Hashtags not loaded...", "error");
    }
);
});
}

casper.then(function() {

var self = this;

// Wait for Logo to load
self.waitForSelector('.Icon--bird',
    function pass() {
        self.echo("*** PASS: Logged in, page loaded");
        
        // Entering main automation logic
        likeTopTweets(self);
    },
    function fail() {
     self.capture("failed2.png");
        self.log("Page FAILED to load", "error");
    }
);  
});

function restartLogic(){
likeTopTweets();
}

// Restart on error
casper.on('error', function(msg, backtrace) {
restartLogic();
});

// Restart on end
casper.run(function() {
    restartLogic();
});

Mark Moffat

unread,
May 23, 2016, 1:34:00 AM5/23/16
to phantomjs
I have the exact same issue. Did you end up figuring this out?
Message has been deleted
Message has been deleted

terry...@gmail.com

unread,
May 29, 2016, 3:32:39 PM5/29/16
to phantomjs
Same issue here. Have spend hours without success.

terry...@gmail.com

unread,
May 30, 2016, 7:13:29 AM5/30/16
to phantomjs
I think it does in fact click on the element. When hovering over them they do change color.
But the pop up window that normally pops up after click on these elements (reply, retweet, like etc) in any real browser doesn´t show up.
Might has to do with the js engine?
Reply all
Reply to author
Forward
0 new messages