Re: www::mechanize question



Jason Roth wrote:

I'm using www::mechanize to submit a form to a website, and one of
the fields is disabled (and enabled by javascript on the page which
obviously isn't running). When I try to set a value for this field I
get a "no such field" error. How to I set a value for, and enable,
disabled form fields?

Hi Jason

WWW::Mechanize subclasses HTML::Form to handle the forms on a page. Ideally you
would add an input to one of these objects, but they're designed to be created
only from parsed HTML.

The best way I can suggest is to create a new HTML::Form object from a scrap of
HTML using the additional input field that the Javascript creates, find the
HTML::Form::Input object in that and add it to the form on the original page.
Fortunately an HTML::Form::Input object has an add_to_form method. Here's a
little subroutine that does just that

sub add_input {
my $form = shift;
my $html = shift;
my $newform = HTML::Form->parse("<form>$html</form>", 'http://void');
my ($input) = $newform->inputs;
$input->add_to_form($form);
}

and if you call it like the program below, hopefully you'll be able to add the
input field that you need. It abuses the OO interface a little but it's the best
I can come up with.

HTH,

Rob




use strict;
use warnings;

use WWW::Mechanize;

my $mech = WWW::Mechanize->new;

$mech->get('http://search.cpan.org');
my $form = $mech->form_number(1);

add_input($form, '<input type="text" name="newfield">');

$mech->field(newfield => 42);

$form->dump;




.



Relevant Pages

  • Re: Adding successive input fields - best practices?
    ... I'd like to be able to put most of the HTML somewhere in a hidden div, ... the index of the "name" field for each new input field. ... Well assuming you didn't have the forms packed into the javascript ...
    (comp.lang.javascript)
  • Re: www::mechanize question
    ... HTML using the additional input field that the Javascript creates, ... my $form = shift; ... First argument is the input type ('text', 'password', ...
    (perl.beginners)
  • Re: Adding successive input fields - best practices?
    ... I'm writing a Rails app that uses some Javascript to allow users to ... I'd like to be able to put most of the HTML somewhere in a hidden div, ... the index of the "name" field for each new input field. ... newInput.name = newId; ...
    (comp.lang.javascript)
  • Checking input fields(text) value when field is loosing focus
    ... I have a input field in my html page. ... Are there any Javascript that can handle this with Regular Expressions? ...
    (comp.lang.javascript)
  • Security holes in Hotmail, Yahoo, and other webmails
    ... Most webmails services and applications have huge security holes on the ... execution of malicious javascript and HTML code ... some parts of the user's mailbox, without use of javascript. ... Cross-site scripting vulnerabilities on the yahoo.com domain was reported ...
    (Vuln-Dev)