perlweb-scrapingwww-mechanizewww-mechanize-firefox

WWW::Mechanize::Firefox clicking or selecting an option from a select?


How do you click or select an option in a select using WWW::Mechanize::Firefox. The select is within a form in this case.

Code follows:

<form name="formo">
<select class="selectc" onchange="go()" name="selecto">
<option value="">Option 1</option>
<option value="">Option 2</option>
<option value="?place1">Option 3</option>
<option value="?place2">Option 4</option>

I would like to click Option 3 to go to place1, for example.


Solution

  • You have to use $mech->select like this:

    $mech->select( 'selecto', '?place1' );
    

    The first parameter is the name of the select element, the second is the value you want to set it to. That value has to be one of the options inside the select. See the docs for more info.