title: How to bypass Mechanize "AmbiguityError" in Python
body: <p>I am trying to upload images to ImageBam by filling its web-forms and requesting POST.</p>
  
  <p>I don't know too much about urllib2, httplib, multipart stuff. I am trying to use MECHANIZE module
  But I think it shouldn't be too complex because it is just a web form, I will fill it and post it.</p>
  
  <p>The page, where upload forms are:
  <a href="http://www.imagebam.com/basic-upload" rel="nofollow">http://www.imagebam.com/basic-upload</a></p>
  
  <p>The form I am trying to fill:</p>
  
  <pre><code>&lt;form name='form' id='form' enctype="multipart/form-data" method="post" action="/sys/upload/save"&gt;
  
  
      &lt;table align="center"&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  01: &lt;input type="file" name="file[]" size="30"&gt;&lt;br&gt;
                  02: &lt;input type="file" name="file[]" size="30"&gt;&lt;br&gt;
                  03: &lt;input type="file" name="file[]" size="30"&gt;&lt;br&gt;
                  04: &lt;input type="file" name="file[]" size="30"&gt;&lt;br&gt;
                  05: &lt;input type="file" name="file[]" size="30"&gt;&lt;br&gt;
  </code></pre>
  
  <p>also I saw a guy created an app using python;
  <a href="http://sourceforge.net/projects/pymguploader/files/pymguploader/2011-12-24/" rel="nofollow">http://sourceforge.net/projects/pymguploader/files/pymguploader/2011-12-24/</a>
  I want to write something like that, but much more basic of course.</p>
  
  <hr>
  
  <p>anyway, here is my problem;
  when I execute these;</p>
  
  <pre><code>import mechanize
  a=mechanize.Browser()
  a.open("http://www.imagebam.com/basic-upload")
  forms=mechanize.ParseResponse(response)
  a.select_form(nr=0)
  dosya=open("file path...","r")
  </code></pre>
  
  <p>everything works fine I think.</p>
  
  <p>also</p>
  
  <pre><code>print a
  </code></pre>
  
  <p>gives this output:</p>
  
  <pre><code>&lt;Browser visiting http://www.imagebam.com/basic-upload
   selected form:
   &lt;form POST http://www.imagebam.com/sys/upload/save multipart/form-data
    &lt;FileControl(file[]=&lt;No files added&gt;)&gt;
    &lt;FileControl(file[]=&lt;No files added&gt;)&gt;
    &lt;FileControl(file[]=&lt;No files added&gt;)&gt;
    &lt;FileControl(file[]=&lt;No files added&gt;)&gt;
    &lt;SelectControl(content_type=[*x, 1, 0])&gt;
    &lt;SelectControl(thumb_size=[*100, 150, 180, 250, 300, 350])&gt;
    &lt;SelectControl(thumb_aspect_ratio=[crop, *resize])&gt;
    &lt;SelectControl(thumb_file_type=[gif, *jpg])&gt;
    &lt;CheckboxControl(thumb_info=[1])&gt;
    &lt;CheckboxControl(gallery_options=[*1])&gt;&gt;
  &gt;
  </code></pre>
  
  <p>but when I</p>
  
  <pre><code>a["file[]"]=dosya
  </code></pre>
  
  <p>the error is;</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;pyshell#46&gt;", line 1, in &lt;module&gt;
      a["file[]"]=dosya
    File "build\bdist.win32\egg\mechanize\_form.py", line 2780, in __setitem__
      control = self.find_control(name)
    File "build\bdist.win32\egg\mechanize\_form.py", line 3101, in find_control
      return self._find_control(name, type, kind, id, label, predicate, nr)
    File "build\bdist.win32\egg\mechanize\_form.py", line 3183, in _find_control
      raise AmbiguityError("more than one control matching "+description)
  AmbiguityError: more than one control matching name 'file[]'
  </code></pre>
  
  <p>How can I solve this problem?</p>
  
  <p><strong>SOLVED</strong></p>
  
  <p>Solution:</p>
  
  <p>a.add_file(dosya,"filename",nr=0)</p>
  
  <p>that automatically searches type=file inputs and adds my file to first one(nr=0 provides it)</p>
  
  <p><strong>New Problem</strong>
  After I sending POST data (or I think it sends)
  This page comes as a response;</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://developers.facebook.com/schema/" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="en"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt;
  &lt;meta name="author" content="ImageBam.com" /&gt;
  &lt;meta name="description" content="Free Image Hosting and photo sharing. Create an online album with bulk upload tools and share with family and friends." /&gt;
  &lt;meta name="keywords" content="image hosting, free image hosting, photo sharing, upload photo, free photo gallery, photo host, image gallery" /&gt;
  &lt;meta name="robots" content="follow" /&gt;
  &lt;meta name="revisit-after" content="1 days" /&gt;
  &lt;meta property="fb:admins" content="3433880" /&gt;
  &lt;link rel="stylesheet" href="http://www.imagebam.com/style.css" type="text/css" /&gt;
  &lt;link rel="shortcut icon" href="http://www.imagebam.com/favicon.ico" /&gt;
  &lt;title&gt;Fast, Free Image Hosting - ImageBam&lt;/title&gt;
  &lt;script type="text/javascript" src="http://www.imagebam.com/JS/imagebam.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="http://www.imagebam.com/JS/pt.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;!-- IMAGEBAM HEADER --&gt;
  &lt;div class="scrollme"&gt;
  &lt;div class="abody"&gt;
  &lt;!-- everything --&gt;
  &lt;div class="banner cursor" style="float:left;" onclick='top.location="http://www.imagebam.com"'&gt;&lt;/div&gt;
  &lt;div style="float:right; text-align:right; border:0px solid #f2f2f2; border-top:none; padding-top: 5px; padding-left:3px; padding-right:10px;"&gt;
  &lt;/div&gt;
  &lt;div style="clear:left;"&gt;&lt;/div&gt;
  &lt;div class="dtab"&gt;
  &lt;ul&gt;
  &lt;li class="inactive"&gt;&lt;a href="/"&gt;Multi-Upload&lt;/a&gt;&lt;/li&gt;
  &lt;li class="inactive"&gt;&lt;a href="/zip-upload"&gt;Zip-Upload&lt;/a&gt;&lt;/li&gt;
  &lt;li class="inactive"&gt;&lt;a href="/basic-upload"&gt;Basic Upload&lt;/a&gt;&lt;/li&gt;
  &lt;li class="inactive"&gt;&lt;a href="/learn-more"&gt;Learn More&lt;/a&gt;&lt;/li&gt;
  &lt;li class="inactive"&gt;&lt;a href="/frequently-asked-questions"&gt;FAQ&lt;/a&gt;&lt;/li&gt;
  &lt;li class="inactive"&gt;&lt;a href="/register"&gt;Register&lt;/a&gt;&lt;/li&gt;
  &lt;li class="inactive"&gt;&lt;a href="/login"&gt;Login&lt;/a&gt;&lt;/li&gt;
  &lt;li class="inactive"&gt;&lt;a href="/premium"&gt;Premium&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
  &lt;/div&gt;&lt;br /&gt;
  &lt;!-- Google Code for Imagebam Uploaded Image Conversion Page --&gt;
  &lt;script type="text/javascript"&gt;
  /* &lt;![CDATA[ */
  function img404(ID,fsrc){
      document.getElementById('thumb_404_info').style.display="block";
      document.getElementById("img_"+ID).style.display = "none";
      document.getElementById("alt_"+ID).style.display = "inline";
      setTimeout("reloadImg("+ID+",'"+fsrc+"')", 500);
  }
  
  function reloadImg(ID,fsrc){
      mrand = Math.random();
      document.getElementById("img_"+ID).style.display = "inline";
      document.getElementById("alt_"+ID).style.display = "none";
      document.getElementById("img_"+ID).src = fsrc+"?"+mrand;
  }
  
  
  /* ]]&gt; */
  &lt;/script&gt;
  &lt;div class="box_wait" style="text-align:center; display:none;" id="thumb_404_info"&gt;Thumbnails that are being processed in the background might not load right away.&lt;br /&gt;&lt;/div&gt;
  &lt;div style="text-align:center; margin-bottom:5px;"&gt;
  &lt;a href="http://videobam.com" target="_blank" style="font-weight:bold; font-size:22px; text-align:center; color:#0174B3;"&gt;&lt;b&gt;NEW!&lt;/b&gt; VideoBam.com (HD Video Hosting)&lt;/a&gt;
  &lt;/div&gt;
  &lt;fieldset&gt;&lt;legend&gt;&lt;img src="/img/icons/photos.png" alt="" style="vertical-align:middle; line-height:16px; height:16px; padding-right:5px;" /&gt; All at Once&lt;/legend&gt;
  &lt;table style="width:100%;"&gt;&lt;tr&gt;
  &lt;td&gt;
  &lt;b&gt;BB-Code&lt;/b&gt;&lt;br /&gt;
  &lt;textarea onclick="this.select();" style="width:300px; height:200px;"&gt;&lt;/textarea&gt;
  &lt;/td&gt;
  &lt;td&gt;
  &lt;b&gt;HTML-Code&lt;/b&gt;&lt;br /&gt;
  &lt;textarea onclick="this.select();" style="width:300px; height:200px;"&gt;&lt;/textarea&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/fieldset&gt;
  &lt;!--
  &lt;fieldset&gt;&lt;legend style='color:green;'&gt;&lt;img src='/img/icons/new.png' alt='' style='vertical-align:middle; line-height:16px; height:16px; padding-right:5px;'&gt; NEW! ImageBam Remote Upload Widget&lt;/legend&gt;
  &lt;b&gt;Webmasters / Mods!&lt;/b&gt;&lt;br&gt; Allow your users to upload images to ImageBam &lt;b&gt;without leaving your website or forum!&lt;/b&gt;&lt;br&gt; Add our new &lt;a href="/remote-upload-widget" target="_blank"&gt;ImageBam Remote Upload Widget&lt;/a&gt; to you website!&lt;br&gt;
  Please spread the word! Thank you!
  &lt;/fieldset&gt;
  --&gt;
  &lt;div style="text-align:center; margin-bottom:5px;"&gt;
  &lt;a href="http://videobam.com" target="_blank" style="font-weight:bold; font-size:22px; text-align:center; color:#0174B3;"&gt;&lt;b&gt;NEW!&lt;/b&gt; VideoBam.com (HD Video Hosting)&lt;/a&gt;
  &lt;/div&gt;
  &lt;fieldset&gt;&lt;legend&gt;&lt;img src="/img/icons/delete.png" alt="" style="vertical-align:middle; line-height:16px; height:16px; padding-right:5px;" /&gt; All Removal Links&lt;/legend&gt;
      Do not share the links below. You can use them to delete the photos you have uploaded.&lt;br /&gt;
  &lt;textarea onclick="this.select()" style="width:600px; height:200px;"&gt;&lt;/textarea&gt;
  &lt;/fieldset&gt;
  &lt;!-- Google Code for Imagebam Uploaded Image Conversion Page --&gt;
  &lt;script type="text/javascript"&gt;
  /* &lt;![CDATA[ */
  var google_conversion_id = 1068053810;
  var google_conversion_language = "en_US";
  var google_conversion_format = "3";
  var google_conversion_color = "ffffff";
  var google_conversion_label = "6tqpCPa-chCy6qT9Aw";
  var google_conversion_value = 0;
  /* ]]&gt; */
  &lt;/script&gt;
  &lt;script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js"&gt;
  &lt;/script&gt;
  &lt;noscript&gt;
  &lt;div style="display:inline;"&gt;
  &lt;img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/1068053810/?label=6tqpCPa-chCy6qT9Aw&amp;amp;guid=ON&amp;amp;script=0" /&gt;
  &lt;/div&gt;
  &lt;/noscript&gt;
  &lt;/div&gt;
  &lt;div class="footer"&gt;
  &lt;a class="footera" href="http://www.imagebam.com/"&gt;ImageBam&lt;/a&gt; | &lt;a class="footera" href="/remote-upload-widget"&gt;Remote Upload Widget&lt;/a&gt; | &lt;a class="footera" href="http://www.imagebam.com/screengrab_upload_firefox_extension"&gt;screengrab tool&lt;/a&gt; | &lt;a class="footera" href="http://www.imagebam.com/terms-of-service"&gt;terms of service&lt;/a&gt; | &lt;a class="footera" href="http://www.imagebam.com/frequently-asked-questions"&gt;help&lt;/a&gt; | &lt;a class="footera" href="http://support.imagebam.com" target="_blank"&gt;support forums&lt;/a&gt; | &lt;a class="footera" href="http://code.google.com/p/imagebam-api/"&gt;API for developers&lt;/a&gt; | &lt;a class="footera" href="http://www.imagebam.com/report-abuse"&gt;report abuse&lt;/a&gt;
  &lt;div style="height:35px; overflow:hidden;"&gt;
  &lt;div id="google_translate_element" style="margin-top:9px;"&gt;&lt;/div&gt;&lt;script type="text/javascript"&gt;
  function googleTranslateElementInit() {
    new google.translate.TranslateElement({
      pageLanguage: 'en'
    }, 'google_translate_element');
  }
  &lt;/script&gt;&lt;script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit" type="text/javascript"&gt;&lt;/script&gt;
  &lt;/div&gt;
  &lt;div style="text-align:center; color:#999; margin-top:10px;"&gt;
  &lt;table style="margin:auto;"&gt;&lt;tr&gt;&lt;td&gt;&lt;img src="http://1.imagebam.com/static/img/tux.png" alt="tux" /&gt;&lt;/td&gt;&lt;td&gt;Powered by dedicated Linux servers. Flixya Entertainment, LLC &amp;copy; 2010&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;div id="updater_index"&gt;&lt;/div&gt;
  &lt;script type="text/javascript"&gt;
  &lt;/script&gt;
  &lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
  &lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  _uacct = "UA-2424497-2";
  urchinTracker();
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Normally, it is the after-uploading page that comes out with image's links etc.
  But I think there is a dynamic process, because the links were not prepared when I got the page.</p>
  
  <p>Am I missing something? because even if you dont fill the inputs on form, if you submit() it, it redirects you to that after-uploading page..</p>