2

I am using WWW::Mechanize::Firefox to automate some interaction with a web page. Evertyhing works well until I reach a page where I want the script to enter a specific value into an <input...> box. Unfortunatly, this page has three <input... boxes with exaclty the same name:

<input name="search_term" value="" class="inputbox" type="text">

The line in the perl script used to fill the value is

$mech -> field('search_term', $value_search);

Since the name search_term does not identify exactly one <input> tag, the script halts with this error message:

3 elements found for input with name 'search_term' ....

So, is there a way to indicate which of the three <input> I want to fill?

Edit

ThisSuitIsBlackNot points out that according to the documentation I should be able to set a third parameter to indicate which of multiple matching elements I refer to. Obviously, this third parameter is ignored so that

$mech -> field('search_term', $value_search, 1);

still halts with the same error message as without explicitely setting the third parameter.

René Nyffenegger
  • 35,550
  • 26
  • 140
  • 232
  • Unless your inputs have IDs, the only way I can see to do this is with [`eval`](https://metacpan.org/pod/WWW::Mechanize::Firefox#mech-eval_in_page-str-env-document). I can't test, but it seems like it should work. – ThisSuitIsBlackNot Jun 27 '15 at 14:05
  • I've never used `WWW::Mechanize::Firefox`, but I'd consider switching to [Selenium::Remote::Driver](http://search.cpan.org/perldoc?Selenium::Remote::Driver). – reinierpost Jun 27 '15 at 21:22

1 Answers1

1

I managed to get it working with eval_in_page:

$mech -> eval_in_page(
  'document.getElementsByName("search_term")[0].value = "' . $value_search . '"'
);
René Nyffenegger
  • 35,550
  • 26
  • 140
  • 232