On Fri, 30 Mar 2007 20:45:43 +0100, Rob Dixon wrote:

> 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);
> }

There's an undocumented method of HTML::Form called push_input that does
just that.  First argument is the input type ('text', 'password',
'hidden', etc), second argument is a hashref of the input attributes. 
Easy for the simple types.  I use it in the same situation:

$mech->current_form->push_input( text => { name => 'hiddeninput' } );

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to