The last line you tried should work if you wrap it inside of a hash -- assuming there is an event that is triggered by clicking the div you've selected. The WWW::Mechanize::Firefox documentation mentions that you have to use the hash along with the 'dom' key. The example below adds a custom event to ensure that there is an event tied to clicking of the div:

use strict; use warnings; use diagnostics; use feature 'say'; use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new( autodie => 1 ); # Load up google search $mech->autoclose_tab(0); $mech->get('http://google.ca'); say "Google retrieved" if $mech->success(); # Make sure the bottom line turns red if we successfully click the <di +v> my $str = " var tmp = document.getElementById('prm-pt'); tmp.onclick = function() { this.style.backgroundColor = 'red'; }; "; $mech->eval_in_page( $str ); say "Event set" if $mech->success(); # Type something in the search box $mech->form_id( 'tsf' ); $mech->set_fields( q => 'time' ); # Click the div, the bottom line should turn red my @divs = $mech->xpath('//div[@id="prm-pt"]'); $mech->click( { dom => $divs[0], synchronize => 0 } ); say "<div> clicked" if $mech->success();

Now that I think of it, all that was missing from your first example (with 'xpath' key) was having the parameters wrapped inside of curly brackets {}. Without them, the synchronize key is ignored, and resulted in an infinite wait.


In reply to Re: WWW::Mechanize::Firefox clicking on a div doesn't work by Loops
in thread WWW::Mechanize::Firefox clicking on a div doesn't work by bbrown25

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.