How to input text into Facebook's event form with WWW::Mechanize::Firefox?

226 views Asked by At

Facebook has no API for submitting an event to a Facebook page. So I'm attempting to use WWW::Mechanize::Firefox with this script.

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    => '[email protected]',
      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"]' });

The form generated by Facebook does not make it easy to input data into the text fields. The fields have no name property and they are not distinguishable with a css selector (though they can be selected with an xpath) as seen in last line.

Because there is no way to input text into the fields, Facebook throws errors because it sees the fields as blank even though I successfully put values into the fields using $mech->field.

Is there any way to get text into Facebook's form for adding events?

UPDATE I made a little progress on this. I was able to at least get some of the fields filled out (Event name and Location) using this code:

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";

Unfortunately, when I click on the "Publish" button with this code:

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

the Event Name and Location text fields get cleared as if nothing was entered. Not sure what else to try at this point.

0

There are 0 answers