Skip to content

Instantly share code, notes, and snippets.

@reyjrar
Created October 15, 2010 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reyjrar/628553 to your computer and use it in GitHub Desktop.
Save reyjrar/628553 to your computer and use it in GitHub Desktop.
Mech/TreeBuilder Example
#!/usr/bin/perl
#
# Code to turn the security report into
# a tab delimited text file.
#
# Code By Brad Lhotsky <lhotskyb@mail.nih.gov>
#
# use at your own risk, no guarantees or warranties associated.
# distributed under the same license as perl itself
use strict;
use WWW::Mechanize;
use HTML::TreeBuilder;
#------------------------------------------------------------------------#
# CONFIGURATION SECTION
#------------------------------------------------------------------------#
my %login = (
url => 'http://my.example.com',
form => 'form1',
fields => {
nedid1 => '000', # CHANGE ME
nedid2 => '1111', # CHANGE ME
nedid3 => '222', # CHANGE ME
txtPassword => 'mypassword', # CHANGE ME
},
);
my %search = (
form => 'form1',
#
# These are all checkboxes. Use a view Source on the Report Query Page to
# retrieve the name of any checkboxes you'd like ticked.
ticks => [qw(
cb_usertype_EMPLOYEE cb_usertype_CONTRACTOR cb_usertype_FELLOW cb_usertype_GUEST
cb_usertype_TENANT cb_usertype_VOLUNTEER cb_display4 cb_display6
)],
submit => 'btnSubmit',
);
my %report = (
filename => '/tmp/Report.txt', # CHANGE ME
);
#------------------------------------------------------------------------#
#
# Assemble our Bot
my $mech = WWW::Mechanize->new();
#------------------------------------------------------------------------#
# Login:
#------------------------------------------------------------------------#
$mech->get($login{url});
# select the form
$mech->form_name( $login{form} );
# fill in the fields:
foreach my $k (keys %{ $login{fields} }) {
$mech->field( $k, $login{fields}->{$k} );
}
# stumbit the form:
$mech->submit();
print "Logged In..\n";
#
# Goto the Report Query
$mech->click( 'btnReportQuery' );
print "Report Query Loaded, filling in values.. \n";
#------------------------------------------------------------------------#
# Fill out the report query
#------------------------------------------------------------------------#
$mech->form_name( $search{form} );
# Tick things on.
foreach my $f (@{ $search{ticks} }) {
#print "$f -> ", $mech->value($f,1), "\n";
$mech->tick($f,'on');
}
#
# Stumbit
$mech->click( $search{submit} );
print "Report Generated, parsing and formatting output..\n";
#------------------------------------------------------------------------#
# Parse the Tree and Turn it into TXT
#------------------------------------------------------------------------#
open( my $fh, '>', $report{filename} ) or die "unable to open $report{filename}: $!\n";
my $tree = HTML::TreeBuilder->new_from_content( $mech->content );
$tree->elementify();
my $tbl = $tree->look_down( 'id', 'reportDataGrid' );
foreach my $tr ( $tbl->find( 'tr' ) ) {
my @row;
foreach my $td ( $tr->find( 'td' ) ) {
if( my $cb = $td->look_down( 'type', 'checkbox' ) ) {
my $val = lc($cb->attr('checked')) eq 'checked' ? 'Y' : 'N';
push @row, $val;
}
else {
push @row, $td->as_text();
}
}
print $fh join("\t", @row), "\n";
}
print "Report Saved\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment