title: Select iframe using Python + Selenium
body: <p>So, I was absolutely baffled as to how to do this in Selenium, and couldn't find the answer anywhere, so I'm sharing my experience.</p>
  <p>I was trying to select an iframe and having no luck (or not repeatably anyway). The HTML is:</p>
  <pre><code>&lt;iframe id=&quot;upload_file_frame&quot; width=&quot;100%&quot; height=&quot;465px&quot; frameborder=&quot;0&quot; framemargin=&quot;0&quot; name=&quot;upload_file_frame&quot; src=&quot;/blah/import/&quot;&gt;
  &lt;html&gt;
      &lt;body&gt;
          &lt;div class=&quot;import_devices&quot;&gt;
              &lt;div class=&quot;import_type&quot;&gt;
                  &lt;a class=&quot;secondary_button&quot; href=&quot;/blah/blah/?source=blah&quot;&gt;
                      &lt;div class=&quot;import_choice_image&quot;&gt;
                          &lt;img alt=&quot;blah&quot; src=&quot;/public/images/blah/import/blah.png&quot;&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;import_choice_text&quot;&gt;Blah Blah&lt;/div&gt;
                  &lt;/a&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The Python code (using the selenium library) was trying to find this iframe using this:</p>
  <pre><code>    @timed(650)
  def test_pedometer(self):
      sel = self.selenium
      ...
      time.sleep(10)
      for i in range(5):
          try:
              if sel.select_frame(&quot;css=#upload_file_frame&quot;): break
          except: pass
          time.sleep(10)
      else: self.fail(&quot;Cannot find upload_file_frame, the iframe for the device upload image buttons&quot;)
  </code></pre>
  <p>Repeated fails with every combination of Selenium commands I could find.</p>
  <p>The occasional success would not be reproducible, so perhaps it was some sort of race condition or something? Never did find the right way to get it in selenium proper.</p>