in reply to Redirecting Forms with WWW::Mechanize

Can you post a larger code sample? Redirects work fine for me, but you'd expect them to. :-)

xoxo,
Andy

  • Comment on Re: Redirecting Forms with WWW::Mechanize

Replies are listed 'Best First'.
Re: Re: Redirecting Forms with WWW::Mechanize
by Ovid (Cardinal) on Sep 30, 2003 at 22:23 UTC

    To make a long story very short, it turns out that there was more than one button on the form. I didn't realize this. The redirect works perfectly. I merely had to select the correct button.

    $webtest->submit_form( form_name => 'lookup', button => 'do_search', fields => { title_name => 'Suspects', });

    Everyone be sure to /msg Ovid RTFM :)

    Cheers,
    Ovid

    New address of my CGI Course.

      That's why I have the mech-dump program included with Mech. Run it against the page you're interested in and you'll have a handy summary of what's going on in the page.

      xoxo,
      Andy

        Andy, Can you tell me how mech-dump works? I'm trying to post a reply to a friends forum. I get the page like this
        get("http://user:passwd\@www.somesite.com/subdir/_vti_bin/shtml.exe/po +st.htm?474")
        I checked response() and this works fine. I get the form and fill in the field like this.
        $w->form_number(1); $w->field('Comments', 'woo-hoo');
        Again, this works fine. The button to post the reply isn't named so I do this.
        $w->submit_form();
        Then I check the response
        my $r = $w->response(); print 'error_as_HTML: '.$r->error_as_HTML."\n";
        and get a "401 Authorization Required" error.

        I think the problem is that the ?474 in the url is a dynamic number assigned by the shtml code. That's one thing I have NO idea how to automate. I don't know the logic on how Microsoft generates that number or on how I can make the post.

        Do you have any idea? Or can you point me in the right direction? I've given up.

        Thanks,
        monktim

Re: Re: Redirecting Forms with WWW::Mechanize
by Ovid (Cardinal) on Sep 30, 2003 at 21:04 UTC

    Here's the basics of how the web testing object is set up. If WWW::Mechanize should automatically follow a redirect request, then perhaps there's something wrong I am doing in the setup?

    package Foo::Webtest; use strict; use warnings; use URI; use Hook::LexWrap; use WWW::Mechanize; use Class::MethodMaker ( new => 'new', object => [ 'WWW::Mechanize' => { slot => 'browser', comp_mthds => [qw/ add_header agent_alias back base click content ct current_form field find_all_links find_link follow_link form_name form_number forms get is_html links quiet redirect_ok reload request response set_fields status submit submit_form success tick title untick uri /], } ]); my $start_time; my @times; my %URI; if ($ENV{TRACK_NAVIGATION}) { foreach my $method (qw/back click follow_link get reload submit subm +it_form/) { wrap $method, pre => sub {$start_time = time}, post => sub { my $end_time = time; my $time = $end_time - $start_time; push @times => $time; push @{$URI{$_[0]->base_uri}} => $time; } } }

    The above code is my setup for the Mechanize object. I use Class::MethodMaker to ensure that I have a "hasa" relationship to Mechanize and all methods listed are delegated to the Mechanize object. Then, Hook::LexWrap is called to allow me to (loosely) time the fetch methods.

    Later, I can do this:

    my $webtest = Foo::Webtest->new; $webtest->get($page_to_test); # and then use the code in my root post

    (Note that this was pared down fairly quickly). I am wondering if my weird setup is causing the problem. All I know right now is that I receive a redirect, but the $webtest->content still points to the page the original form is on. I was hoping it was simply a quick question on how to get the form to automatically follow, but if it's not, I can spend some time paring this down to a minimal test case.

    Cheers,
    Ovid

    New address of my CGI Course.