WWW:Mechanize Form Select

2k views Asked by At

I am attempting to login to Youtube with WWW:Mechanize and use forms() to print out all the forms on the page after logging in. My script is logging in successfully, and also successfully navigating to Youtube.com/inbox; However, for some reason Mechanize can not see any forms at Youtube.com/inbox. It just returns blank. Here is my code:

#!"C:\Perl64\bin\perl.exe" -T

use strict;
use warnings;

use CGI;
use CGI::Carp qw/fatalsToBrowser/;
use WWW::Mechanize;
use Data::Dumper;

my $q = CGI->new;

$q->header();

my $url = 'https://www.google.com/accounts/ServiceLogin?uilel=3&service=youtube&passive=true&continue=http://www.youtube.com/signin%3Faction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252Findex&hl=en_US&ltmpl=sso';

my $mechanize = WWW::Mechanize->new(autocheck => 1);

$mechanize->agent_alias( 'Windows Mozilla' );

$mechanize->get($url);

$mechanize->submit_form(
        form_id => 'gaia_loginform',
        fields      => { Email => 'myemail',Passwd => 'mypassword' },
    );
    die unless ($mechanize->success);

$url = 'http://www.youtube.com/inbox';

$mechanize->get($url);

$mechanize->form_id('comeposeform');

my $page = $mechanize->content();

print Dumper($mechanize->forms());

Mechanize is unable to see any forms at youtube.com/inbox, however, like I said, I can print all of the forms from the initial link, no matter what I change it to...

Thanks in advance.

1

There are 1 answers

3
Alessandro On BEST ANSWER

As always, one of the best debugging approaches is to print what you get and check if it is what you were expecting. This applies to your problem too.

In your case, if you print $mechanize->content() you'll see that you didn't get the page you're expecting. YouTube wants you to follow a JavaScript redirect in order to complete your cross-domain login action. You have multiple options here:

  • parse the returned content manually – i.e. /location\.replace\("(.+?)"/
  • try to have your code parse JavaScript (have a look at WWW::Scripter)
  • [recommended] use YouTube API for managing your inbox