点击关注者页面,点击CasperJS环境中Twitter的关注者取消关注按钮

时间:2015-10-14 16:52:55

标签: javascript casperjs

我实际上想在CasperJS环境中关注/取消关注Twitter帐户。在编写JavaScript代码之前,我习惯使用导航器(例如使用Firefox或Chrome)进行一些测试。我尝试了几种按钮单击语法,它们在导航器中运行良好,但在CasperJS环境中却没有。

例如,我使用Firefox登录Twitter,然后转到此网址" https://twitter.com/googledrive" (这是我已经关注的帐户)。我将鼠标移到"关注/取消关注"按钮并右键单击以检查按钮元素。导航器打开一个控制台(就像F12一样)。然后我右键单击元素(在HTML代码中)并选择"复制唯一选择器"。然后我只需在控制台栏中写下以下行:

document.querySelector("span.button-text:nth-child(3)").click();

甚至这个有效:

document.querySelector(".button-text.unfollow-text").click();

当我返回以下列表时,Google云端硬盘帐户已被删除。

我想在CasperJS环境中执行相同的操作。我可以登录,转到包含我关注的帐户的页面,点击Google云端硬盘帐户,当我尝试执行取消关注时,CasperJS不会抛出任何错误,并执行点击操作。但是当我去Firefox并刷新我的页面时,我仍然有相同数量的以下内容......

var casper = require("casper").create({
    verbose: true,
    logLevel: 'debug',
    pageSettings: { userAgent: "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0" }
});

var email = 'My Email';
var auth = 'My Password';

casper.start('https://twitter.com/', function() {
    this.fill('form.t1-form.signin', {
        "session[username_or_email]" : email,
        "session[password]" : auth
    },true);
}); //It leads me to my Twitter Dashboard

//Go to my followings list
casper.then(function(){
    console.log(this.getTitle());
    this.click("div.ProfileCardStats:nth-child(3) > ul:nth-child(1) > li:nth-child(2) > a:nth-child(1)");
    casper.wait(2000);
});

//I click one of the links to Google Drive page
casper.then(function(){
    console.log(this.getTitle());
    this.click("#stream-item-user-32468665 > div:nth-child(1) > div:nth-child(2) > div:nth-child(3) > span:nth-child(2) > a:nth-child(1)");
    casper.wait(2000);
});

//Unfollow action
casper.then(function(){
    console.log(this.getTitle());
    this.click("button.user-actions-follow-button");
    casper.wait(2000);
});

casper.then(function(){
    casper.wait(1000, function(){
        this.back();
    });
    casper.wait(2000);
});

//Then I want to go back to my followings list
casper.then(function(){
    casper.wait(2000, function(){
        console.log(this.getTitle());
    });
});

casper.run();

以下是调试说的内容:

enter image description here这似乎很好,就像按钮存在但点击操作根本不起作用。

我做错了什么?

0 个答案:

没有答案