WWW::Mechanize::Firefox click button in <div>

1.1k views Asked by At

I got a big problem. I have 12 elements with the same class - "comment". In every is button to click.

I need to know how to select (eg. 3 of 12) and click button only there.

Is exist any selector or something? Please give me some advice about that.

1

There are 1 answers

9
ThisSuitIsBlackNot On

$mech->click can take a CSS selector or an XPath query as an argument. If you know that the button you want to click will always be the third one, you could use something like the nth-of-type CSS selector to pinpoint it.

EDIT: An example using XPath based on the OP's comments (I haven't tested this). Use qq to allow variable interpolation inside the XPath statement. Note that you have to backslash escape occurrences of @ so qq doesn't interpret them as arrays:

my $author = 'xxx';
$mech->click({ xpath => qq(//div[\@class="com" and \@author="$author"]/button) });