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

I am wtriting some code to automatically play the UK national lottery. Using perl I am trying to get a click on a button on the form. The html code segment, my code and the output is below. Does anyone have any ideas why the submit does not work, it ends up back at the home page ? In the code you will see i have tried post and click and the results of these efforts.. Any assistance would be much appreciated.

Output:- here1 here1.1 here1.2 here1.3 here2 here2.1 here2.11 here2.12 here2.2 here2.3 here2.31 here2.4 FORM: logoutForm - logoutForm INPUT: hidden - SYNCHRONIZER_TOKEN INPUT: hidden - SYNCHRONIZER_URI INPUT: submit - logout Use of uninitialized value in concatenation (.) or string at auto_play +.pl line 89. Use of uninitialized value in concatenation (.) or string at auto_play +.pl line 89. FORM: - INPUT: hidden - SYNCHRONIZER_TOKEN INPUT: hidden - SYNCHRONIZER_URI INPUT: submit - confirm TOKEN value=[83f5d4fc-fd2b-4139-97a4-54192cc7806c] URI value=[xxxxxxxxxxxx] Returned : HTTP::Response=HASH(0x1da7be0) here2.5 here2.6 End
use warnings; use strict; use WWW::Mechanize; use HTTP::Cookies; my $outfile = "auto_play.tmp"; my $outfile1 = "auto_play.log"; my $url = "<lottery sign in>"; my $username = 'xxxxxxxxxxx'; my $password = 'xxxxxxxxxxx'; my $fragment = ""; my $fragment1 = ""; my $new_url = ""; my $play_url = ""; my $output_page = ""; my $mech = WWW::Mechanize->new(); print "here1\n"; $mech->cookie_jar(HTTP::Cookies->new()); $mech->get($url); sleep(2); print "here1.1\n"; $mech->field("j_username", $username); sleep(2); print "here1.2\n"; $mech->field("j_password", $password); sleep(2); print "here1.3\n"; $mech->submit(); sleep(2); print "here2\n"; $mech->follow_link( url => '/games/lotto'); sleep(1); $mech->form_name("playslip_form_lotto"); print "here2.1\n"; $mech->tick("drawDays", 'WEDNESDAY'); $mech->field("numberWeeks", '1'); $mech->field("line_0_pool_0_col_0", '1'); $mech->field("line_0_pool_0_col_1", '2'); $mech->field("line_0_pool_0_col_2", '3'); $mech->field("line_0_pool_0_col_3", '4'); $mech->field("line_0_pool_0_col_4", '5'); $mech->field("line_0_pool_0_col_5", '6'); sleep(2); print "here2.11\n"; $mech->submit(); print "here2.12\n"; sleep(1); print "here2.2\n"; $mech->form_name("confirm-play-slip"); print "here2.3\n"; $output_page = $mech->content(); open(OUTFILE, ">$outfile"); binmode(OUTFILE, ":utf8"); print OUTFILE "$output_page"; close(OUTFILE); print "here2.31\n"; my $find = 'name="SYNCHRONIZER_TOKEN" value="'; while ($output_page =~ m/$find/g) { my $pstion = pos($output_page); #print("Matched $find at position $pstion\n"); $fragment=substr $output_page,$pstion,36; } my $find1 = 'name="SYNCHRONIZER_URI" value="'; while ($output_page =~ m/$find1/g) { my $pstion1 = pos($output_page); #print("Matched $find at position $pstion1\n"); $fragment1=substr $output_page,$pstion1,30; } print "here2.4\n"; my @forms = $mech->forms; for my $form (@forms) { print STDERR "FORM: ".$form->attr( 'name' ).' - '.$form->attr( 'id' ). +"\n"; my @inputs = $form->inputs; for my $input (@inputs) { print STDERR " INPUT: ".$input->type().' - '.$input->name()."\n"; } } print "TOKEN value=[$fragment]\n"; print "URI value=[$fragment1]\n"; $mech->field("SYNCHRONIZER_TOKEN", $fragment); $mech->field("SYNCHRONIZER_URI", $fragment1); sleep(1); my $rtn = $mech->submit(); print "Returned : $rtn\n"; #my $rtn = $mech->post(); # Also tried + this but got .... # Can't call method "as_string" on an +undefined # value at /usr/local/share/perl5/WWW/ +Mechanize.pm line 2467. #$mech->click('confirm'); # And this b +ut got... # No clickable input with name confirm + at # /usr/local/share/perl5/WWW/Mechanize +.pm line 1742. print "here2.5\n"; $output_page = $mech->content(); print "here2.6\n"; open(OUTFILE, ">$outfile1"); binmode(OUTFILE, ":utf8"); print OUTFILE "$output_page"; close(OUTFILE); print "End\n";
<form action="/games/lotto/confirm-play-slip?p +layId=play_1_2014091611484298" method="post" > <input type="hidden" name="SYNCHRONIZER_TOKEN" value="edaf1847-286c-4c +51-b811-09f592511a34" id="SYNCHRONIZER_TOKEN" /> <input type="hidden" name="SYNCHRONIZER_URI" value="/games/lotto/confi +rm-play-slip" id="SYNCHRONIZER_URI" /> <fieldset class="full_width_m float_right" +> <legend class="vh">Terms and Condition +s, Rules and Game Procedures information</legend> <input type="submit" id="confirm" name +="confirm" class="btn btn_primary float_right" value="Buy now" /> </fieldset> </form>

Replies are listed 'Best First'.
Re: Button Click
by Anonymous Monk on Sep 16, 2014 at 12:08 UTC
    See that bit on the site where it says they don't want you to do this, don't do this. For a human the site already allows you to buy a ticket for weeks, or for ever, it makes no sense to automate when that already exists.
      I run a lottery sindicate and have to enter 15-20 lines of numbers twice a week which the members of the sindicate submit to me via email, so automating it would make my life a lot easier....
        RE "on the site where it says they don't want you to do this" could you give me a URL for the page where this is detailed. I have had a good poke around and cant find it.