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 name
和Location
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 Name
,Location
就好像没有输入任何内容一样。不知道此时还可以尝试什么。