perlwww-mechanize-firefox

WWW::Mechanize::Firefox CSS Selector with multiple elements?


When using WWW::Mechanize::Firefox to select an item, is it possible to iterate through a number of selectors that have the same name?

I use the following code:

my $un = $mech->selector('input.normal', single => 1);

The response is 2 elements found for CSS selector. Is there a way to use XPath or a better method, or is it possible to loop through the results?

Bonus point: typing into the inputs even though it is not in form elements (ie. uses JavaScript)


Solution

  • With the single option you have specified that there should be exactly one element that matches the selector. That is why you get an error message when it finds two matches.

    The method will return a list of matches, and you can either use one => 1 in place of single => 1, which will throw van error if there isn't at least one match, or you can leave the option out altogether, when it will simply return all that it finds.

    my @inputs = $mech->selector('input.normal')
    

    will fill the array @inputs with a list of matching <input> elements, however many there are.