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

Greetings Monks,
I am trying to use WWW::Mechanize to submit some forms to a web site, but the form authors have got it in for me. They have some radio buttons which look a bit like this:
<input type='radio' name='button1' value='button1' onClick='SomeJavasc +riptfunction()' checked> <input type='radio' name='button2' value='button2' onClick='SomeJavasc +riptfunction()'> <input type='radio' name='button3' value='button3' onClick='SomeJavasc +riptfunction()'>
Now, the SomeJavascriptfunction() is designed to make sure that only one of these radio buttons is selected. It makes no sense to me why they didn't just use the same value for each radio button, ensuring that the browser did that for them, but I have no control over the site. So, my question is, how can I uncheck button1 with mech, if I want to check button3? I can't see a 'clear' function anywhere in WWW::Mechanize anywhere. Checking button3 is as easy as
$mech->fields('button3'=>'button3');
But that leaves button1 checked.

Replies are listed 'Best First'.
Re: WWW::Mechanize and clearing radio buttons
by james2vegas (Chaplain) on Aug 24, 2009 at 00:00 UTC
    Yeah, what a badly written piece of HTML, groups of option buttons of which only one should be selected should have the same name.

    The WWW::Mechanize documentation is unclear, but have you tried button1 => undef?

    $mech->fields(button3=>'button3', button2 => undef, button1 => undef);

    or

    $mech->submit_form( fields => { button1 => undef, button2 => undef, button3 => 'button3', } );


    should work. Also, you don't need to quote hash keys that are alphanumeric when you use the fat comma (=>).