Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write a script for myself that when provided with a list of users from youtube I want to request as friends will automatically send the request. I'm baffled why the submit piece from Mechanize doesn't seem to work as I expect. This is my code snippet to submit the form:
... $mech->follow_link( text => "Add as Friend"); my $page = $mech->content; my $hidden_id = $1 if $page =~ m/\<input type\=\"hidden\" name\=\" +friend_id\" value=\"(.*)\"\>/; my $friend_id = 'http://youtube.com/my_friends_invite_user?friend_ +id=' .$hidden_id; # print "FRIEND ID: $friend_id\n"; $mech->add_header( Referer => $friend_id ); $mech->form_number(3); my $form = $mech->form_number(3); $form->find_input('friend_id')->readonly(0); $mech->set_fields(friend_id => $hidden_id); $mech->field('group', 'Friends' ); $mech->click_button(name => 'action_invite');
Everything looks like it gets set properly but it never actually submits the form. I've also posted the source code behind the webpage if that helps below:
<form method="post" action="my_friends_invite_user"> <input type="hidden" name="friend_id" value="JMwzlfI6UQU"> <tr> <td class="formLabel">Username:</td> <td class="formField"><a href="/profile?user=wasteofaces">wasteo faces</a></td> </tr> <tr valign="top"> <td class="formLabel">Add As:</td> <td class="formField"> <select name="group"> <option value="">---</option> <option value="Family" >Family</option> <option value="Friends" SELECTED>Friends</option> </select> <div class="smallText">This person will be able to see the pri +vate videos you share with these groups in addition to your public vi +deos. </div> </td> </tr> <tr> <td class="formLabel">&nbsp;</td> <td class="formField"><input type="submit" name="action_invite" +value="Send Invite"></td> </tr> </form>
I'm obviously missing something so any and all suggestions are welcome.

Replies are listed 'Best First'.
Re: YouTube and Mechanize form submission
by pemungkah (Priest) on Sep 12, 2007 at 20:06 UTC
    User agent would be my first guess. Many sites (Yahoo! included) do different things if they detect a user agent that says "hi, I'm not a real person, but a program". I know this well from trying to write automated test scripts.

    Use $mech->agent_alias('Windows IE 6") and space out your requests with sleep 2 or so. This will be less likely to trigger the "dude, you are so not for real" checks.

Re: YouTube and Mechanize form submission
by mmmmtmmmm (Monk) on Sep 13, 2007 at 01:43 UTC
    Usually just setting the agent name is enough to keep sites from blocking robots, but I also usually tell it to sleep with a random value between 2 and 6 seconds, instead of a hardcoded value, so that it looks more like a human.

    ----mmmmtmmmm
Re: YouTube and Mechanize form submission
by technojosh (Priest) on Sep 12, 2007 at 19:34 UTC
    Everything looks like it gets set properly but it never actually submits the form

    Ok, I'll take a crack at it...

    Since you are running against a remote site, you probably don't have access to errors in the server log to tell you how the POST was handled.

    The html looks ok.

    I am wondering if, since you are running against a remote site, you are having trouble calling the action like

    <form method="post" action="my_friends_invite_user">

    instead of going to the URL as in:

    <form method="post" action="http://youtube.com/my_friends_invite_user">
A reply falls below the community's threshold of quality. You may see it by logging in.