Perl Using WWW::Mechanize to set a radio button that has no value

I have to say I am new to Perl, maybe made about 3 or 4 scripts so far, and I find Perl decently easy to understand and use.

Anyways, I have a problem with my current script. I have been stuck on this problem for a while and just can't seem to find a fix for it.

After traversing a certain website I come to a page that has me click between two radio buttons and hit a download button.

I need to get my script to select either button and hit download, but the source of the page does not give me a value to set the radio button to when using WWW::Mechanize.

Here is a little bit of the web page source.

<input type='radio' onclick="url('URL GOES HERE')">

That is pretty much it for the two buttons. I noticed that when I did choose one and looked at the source, the code changes to

<input type='radio' checked onclick="url('URL GOES HERE')">

Yet I can't have WWW::Mechanize set one automatically because I have no idea what to put in the value. I have tried on, checked, onclick, true, checked onclick, select, selected, but to no avail.

Here is the line of code that pertains to this

$mech->set_visible([radio => 'checked onclick']);

Any help would be appreciated, first time here as well.

I forgot to mention that I am working with a computer at work that has a lot of restrictions for now. So I can't get Firefox or Selenium Server on the computer.

Edit

I believe the problem that I might be having also is that this stuff might be in JavaScript. I don't have much knowledge in HTML but the source code looked like it was until I saw JavaScript somewhere in the heading? Anyways, I remembered that WWW::Mechanize doesn't really support JavaScript and so maybe this is the problem.

Thanks for the replies guys, I really appreciated it. But after a ton of digging around and careful inspection, my boss and I sort of realized that all the download links are very similar, and all I really need to do is make the script custom create the links to the downloads instead of going through the hassle of taversing multiple web pages. Wish I saw this sooner.

Answers:

Answer

HTML::Form does not offer an API to activate unnamed radio buttons. You can read the distinguishing attribute by digging into the innards. This should work (mostly untested):

use WWW::Mechanize qw();
my $w = WWW::Mechanize->new;
$w->get('file:///tmp/so11767998.html');
for my $input (($w->forms)[0]->inputs) {
    $input->check if q{url('URL GOES HERE')} eq $input->{onclick}
}
Answer

You need to find what's happens after you select your radio button in the browser GUI. There are two ways for this:

  1. you can check url() Javascript subroutine

  2. or you can record your browser request after you submit target form.

I recommend to use Firefox's HTTPFox extension for this. So you just select you input, start the session in HTTPFox and submit the form. Next you check what data was POSTed (or GETed) and just replicate same fields in your Mechanize script. Something like:

$mech->submit_form(
    form_name => "form_name",
    fields => {
        input1_name => "value1",
        input2_name => "value2",
.....
    },
);
Answer

I had this issue too , and resolved it by calling a Javascript to click Radio Button.

$mech->eval_in_page('document.getElementById("id_radiobutton").checked = true;');

This worked for me.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.