Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 579
imakeitrayne Avatar asked Jan 19 '26 00:01

imakeitrayne


1 Answers

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}
}
like image 193
daxim Avatar answered Jan 20 '26 14:01

daxim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!