0

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.

Kamil Hajduk
  • 51
  • 1
  • 6

1 Answers1

2

$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) });
ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
  • yes, i know, but this button can be 3 and 10 seconds later can be 4. so i need select div with this button :( is any possible method for that? thanks. – Kamil Hajduk Aug 07 '13 at 15:59
  • @KamilHajduk What determines which button you need to click? – ThisSuitIsBlackNot Aug 07 '13 at 16:02
  • it's look like this:
    ..............
    ..............
    So it's determines by
    section. You have any ideas?
    – Kamil Hajduk Aug 07 '13 at 16:07
  • or maybe this module support something like separate
    by author name and then click button number 1 (the only one in there)
    – Kamil Hajduk Aug 07 '13 at 16:10
  • @KamilHajduk XPath lets you select elements from the DOM using complex criteria. There's a good intro to XPath [here](http://oreilly.com/perl/excerpts/system-admin-with-perl/ten-minute-xpath-utorial.html). See my edit for an example. – ThisSuitIsBlackNot Aug 07 '13 at 16:31
  • Thank you very much! I have one more question. I got xpath query like @divs = $mech->xpath('//button[@class="submit"]', single => 1); and in output that give me 12 elements. Can i determine click in eg. third button? I mean that i got 12 buttons with the same class and i want to click button number three. Is it possible? $mech->click accept xpath but $mech->button_click accept number of button but xpath not :( – Kamil Hajduk Aug 07 '13 at 16:59
  • @KamilHajduk Yes, that is possible. Your XPath would look something like `//button[@class="submit"][3]` (note XPath indexes start with 1, not 0). Please read more about XPath, you can do just about anything with it. – ThisSuitIsBlackNot Aug 07 '13 at 17:10
  • Man you are awesome! I must learn more about this XPath! It's powerfull tools :) I got last question. I got XPath query '//div[@class="comment" and @author="6546678"]/div/div[2]/span[2]/button' and it's working but when i try to do @author="$author_id" - it's not working. I try to change this in qq## and "" but it's still not working. Is it possible to add string in '' ? When i add author id manually, not in string, it's works fine, but not with string. I would like to know how to do this, becouse this author id will be dynamic. – Kamil Hajduk Aug 07 '13 at 17:41
  • @KamilHajduk See edit. If you have additional questions, please open a new question instead of adding comments to this answer. And if my answer solved your problem, you can accept it by clicking the check mark next to it :-) – ThisSuitIsBlackNot Aug 07 '13 at 18:04