如何使用WWW :: Mechanize :: Firefox将文本输入到Facebook的事件表单中?

时间:2016-02-09 04:48:29

标签: facebook perl www-mechanize-firefox

Facebook没有用于向Facebook页面提交活动的API。所以我试图在这个脚本中使用WWW :: Mechanize :: Firefox。

use strict;
use warnings;
use feature qw(say);
use WWW::Mechanize::Firefox;

my $mech = WWW::Mechanize::Firefox->new(activate => 1); 
$mech->autoclose_tab(0);
$mech->get('http://facebook.com');

if ($mech->title eq 'Facebook - Log In or Sign Up') {
  $mech->submit_form(
    with_fields => {
      email    => 'my@email.com',
      pass => 'my_password',
    }   
  );  
}
sleep(1);

$mech->get('https://www.facebook.com/PageName/events');
my $page_id = 777777777777777;

$mech->click({ synchronize => 0, xpath => '//a[text() = "Create Event"]' }, 10, 10);
sleep(3);

# selects all input fields and sets value to 'hello world'
# even though values are set, the fields remain blank
my @selectors = $mech->selector('input');
foreach my $selector (@selectors) {
  $mech->field( $selector, 'hello world');
}

# attempts to publish event, results in form errors because fields are blank
$mech->click({ synchronize => 0, xpath => '//button[text() = "Publish"]' });

# test to see if values are getting set.
# this returns 'hello world' as so values are getting set
say $mech->value($selectors[2]); 

# test to see if an input field can be selected with an xpath
# this works
$mech->click({ synchronize => 0, xpath => '//input[@placeholder="Include a place or address"]' });

Facebook生成的表单无法轻松将数据输入文本字段。这些字段没有名称属性,并且它们无法与css选择器区分(尽管可以使用xpath选择它们),如最后一行所示。

由于无法在字段中输入文本,因此Facebook会抛出错误,因为即使我使用$mech->field成功将值放入字段,它也会将字段视为空白。

有没有办法让文字进入Facebook的表格来添加活动?

更新 我在这方面取得了一些进展。我能够使用以下代码至少填写一些字段(Event nameLocation):

my $field = $mech->xpath( '//input[@placeholder="Include a place or address"]', one => 1);
$field->{value} = 'Room 315, City Hall';

$field = $mech->xpath( '//input[@placeholder="Add a short, clear name"]', one => 1);
$field->__click;
$field->{value} = "License committee meeting";

不幸的是,当我点击带有此代码的“发布”按钮时:

$mech->click({ synchronize => 0, xpath => '//button[text() = "Publish"]' }, 10, 10);

Event NameLocation文本字段被清除,就好像没有输入任何内容一样。不知道还有什么可以尝试。

0 个答案:

没有答案