perlweb-scrapingwww-mechanize-firefox

How do I set a value with WWW::Mechanize::Firefox when there are three <input> tags with the same name?


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.


Solution

  • I managed to get it working with eval_in_page:

    $mech -> eval_in_page(
      'document.getElementsByName("search_term")[0].value = "' . $value_search . '"'
    );