Search code examples
htmlperlwww-mechanize

WWW::Mechanize- Select from drop down list


I am trying to use WWW::Mechanize to select an option from a drop-down menu. Here is the code I have right now (only the part with selecting from the drop down menu):

use warnings;
use WWW::Mechanize;
my $url = 'http://genome.ucsc.edu/cgi-bin/hgBlat?command=start';
my $browser = WWW::Mechanize->new();
  $browser->get($url);
    $browser->select('db', 'hg38');
    $browser->submit();

The selection does not work.

Here is the HTML for this drop-down menu from the site where I've manually selected the desired option:

<select name="db"> = $0
     <option selected value="hg38">Dec. 2013 (GRch38/hg38)</option>
     <option value="hg19">Feb. 2009 (GRch17/hg19)</option>
     <option value="hg18">Mar. 2006 (NCBI36/hg18)</option>
     <option value="hg17">May 2004 (NCBI35/hg17)</option>
     <option value="hg16">July 2003 (NCBI34/hg16)</option>
</select>

How can I use Mech to select the option? The url for the website in question is 'http://genome.ucsc.edu/cgi-bin/hgBlat?command=start'.


Solution

  • You should always use strict at the top of every Perl program you write, especially before asking for help with your code. It would have revealed the problem immediately

    Your WWW::Mechanize object is in $browser, but you have called $mech->select. $mech hasn't been defined so you are getting an error message saying that select can't be called on an undefined value