#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    30
    Rep Power
    2

    WWW::Mechanize returning errors


    I am trying to use WWW::Mechanize in a simple script and it all ways comes up with an error.
    Code:
    #!/usr/bin/perl
    use WWW::Mechanize;
    $url = 'http://www.google.com';
    $m->get($url);
    $m->form_name('f');
    $m->field('q', 'test search');
    $response = $m->submit();
    print $response->content();
    the error is
    Code:
    Can't call method "get" on an undefined value at /home/Documents/spider/spider1/mainspider3.pl line 4.
  2. #2
  3. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,221
    Rep Power
    1809
    What is $m?

    You are hoping it is a WWW::Mechanize object, since you call Mechanize methods on it, but you never defined or instantiated it.

    The essential, and missing line would be:

    Code:
    my $m = WWW::Mechanize->new;
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    68
    Rep Power
    2
    hi guys, im trying to use WWW::Mechanize. Can you please tell me how can i know which form_name("????") and which field('????', 'test search') should i put. Where can i find those variables?

    thanks
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    30
    Rep Power
    2
    I was wondering the same.
  8. #5
  9. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,221
    Rep Power
    1809
    Originally Posted by andreas.london
    hi guys, im trying to use WWW::Mechanize. Can you please tell me how can i know which form_name("????") and which field('????', 'test search') should i put. Where can i find those variables?
    The information is in the HTML source. Just about all browsers allow you to view the source (might be on a developer menu). If not that, then download the page and open it in a text editor.

    If you look at the source for this page for example, you'll find several forms. Some have names, some have ids, some have neither.

    If you look at the WWW::Mechanize methods, you'll see these for selecting forms:

    Code:
    $mech->forms
    $mech->form_number($number)
    $mech->form_name( $name )
    $mech->form_id( $name )
    $mech->form_with_fields( @fields )
    Get all the forms
    Get a form by number
    Get a form by name
    Get a form by id
    Get a form by matching available field names

    That's just to give you the flexibility of finding forms in many different ways. If you know you want the first or second form on the page, you can get it by number. If it has a name, use that. If it has an id, use that.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    68
    Rep Power
    2
    I see. yea, i found the form names and ids!

    Unfortunately i still have an error when im using this code:
    Code:
    #!/usr/bin/perl 
    use WWW::Mechanize; 
    my $mech = WWW::Mechanize->new; 
    $url = 'https://demo01.service-now.com/navpage.do'; 
    
    $mech->get($url); 
    $number = 1; 
    $mech->form_number($number);  
    $mech->field("user_name", "itil"); 
    
    $mech->filed("user_password", "itil");  
    $mech->click(); 
    $mech->get($url);

    ERROR:
    Code:
     Can't call method "value" on an undefined value at C:/Perl/lib/WWW/Mechanize.pm line 1403.
    Thanks a lot.
    Awaiting your reply!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    68
    Rep Power
    2
    also i have this HTML code from a button but i don't know whick value to use. Any idea? Thanks

    Code:
    <a id="lnkHdrnewmsg" class="btn" title="New Message" onclick="return onClkTb('newmsg');" href="#">
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    30
    Rep Power
    2
    I got the error message that you got about "value". I was doing something else but im still having a problem with this error.

IMN logo majestic logo threadwatch logo seochat tools logo