在CasperJS中按类和特定文本单击元素

时间:2016-03-15 00:44:30

标签: javascript css-selectors phantomjs

我正在尝试2小时现在找到一个选择器来在Facebook共享对话框中选择表单:  facebook share dialog

(我不使用他们的API来限制访问某些功能)

=-

第一次点击效果非常好,这是一个截图:

screenshot

但第二次点击不会:

var casper = require('casper').create({

    pageSettings: {
        loadImages:  false,        // The WebPage instance used by Casper will
        loadPlugins: false,        // use these settings
        userAgent: 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0'
    },
    logLevel: "info",              // Only "info" level messages will be logged
    verbose: true


});
var fs = require('fs');
var dyn = null;
casper.start('http://facebook.com/login.php', function() {
    // search for 'casperjs' from google form
   console.log("page loaded");

   this.test.assertExists('form#login_form', 'form is found');

   this.fill('form#login_form', { 
        email: 'email', 
        pass:  'pass'
    }, true);

    this.wait(1000, function() {
        casper.capture('login.png');
        console.log("screen captured");
    });
});
    casper.thenOpen('https://www.facebook.com/dialog/share?app_id=966242223397117&redirect_uri=http://www.facebook.com/dialog/return/close&display=popup&href=http://www.isearchforjob.com/', function() {

    console.log("debug loaded");

    this.wait(1000, function() {
        casper.capture('debug.png');
        console.log("screen captured");
    });

    if (this.exists('span._55pe')) {
        this.echo('span._55pe exists');
        this.click('span._55pe');
        this.echo('\n\nspan._55pe clicked\n\n');
    }   

    this.wait(5000, function() {
    if (this.exists('li[class="_54ni _42ym _2n3i __MenuItem"] > a._54nc')) {
        this.echo('li[class="_54ni _42ym _2n3i __MenuItem"] > a._54nc exists');

        this.echo('\n\n li[class="_54ni _42ym _2n3i __MenuItem"] > a._54nc clicked\n\n');
    }   

    });


    this.wait(10000, function() {

        fs.write('2.htm', this.getPageContent(), 'w');

        casper.capture('clicked2.png');
        console.log("screen captured");
    });
});
casper.run();

为所有列表生成相同的选择器,因此它无用

这是HTML:

this.click('li._54ni _42ym _2n3i __MenuItem');

我有没有机会通过短信获得选择器? “分享成群”

在这种情况下是否可以模拟向下箭头键?

1 个答案:

答案 0 :(得分:0)

这些类都是在使用类选择器后写的:

this.click('li._54ni._42ym._2n3i.__MenuItem');

您还可以使用an attribute selector来匹配具有特定属性字符串的元素(适用于任何属性,而不仅仅是class):

this.click('li[class="_54ni _42ym _2n3i __MenuItem"]');

你甚至可以混合使用:

this.click('li.__MenuItem[class^="_54ni _42ym"]');

最后,您可能希望单击列表项中的链接:

this.click('li[class="_54ni _42ym _2n3i __MenuItem"] > a._54nc');