title: iframe element missing from selenium
body: <p>I am trying to do some automated functionality using python selenium, and I'm coming across some weird behavior.</p>
  <p>General layout of the html:</p>
  <pre><code>&lt;html&gt;
    &lt;body&gt;
      &lt;div class=&quot;parent&quot;&gt;
        &lt;iframe style=&quot;display: none&quot;&gt; ... &lt;/iframe&gt;
        &lt;iframe style=&quot;display: none&quot;&gt; ... &lt;/iframe&gt;
        &lt;iframe style=&quot;display: block&quot;&gt;
          #document
          ...
          &lt;div class=&quot;someClass&quot;&gt; ... &lt;/div&gt;
        &lt;/iframe&gt;
        &lt;iframe style=&quot;display: none&quot;&gt; ... &lt;/iframe&gt;
        &lt;iframe style=&quot;display: none&quot;&gt; ... &lt;/iframe&gt;
      &lt;/div&gt;
    &lt;/body&gt;
  </code></pre>
  <p>Now, each iframe actually has the same inner html, and the code from the website seems to be randomly choosing which iframe is getting the <code>display=&quot;block&quot;</code>. However, I can't find any of the iframes.</p>
  <p>I tried a standard way: <code>iframe = driver.find_elements_by_xpath(&quot;//iframe[contains(@style, 'display:block')]&quot;)</code></p>
  <p>That failing, I then tried just to find any iframe: <code>driver.find_element_by_tag_name(&quot;iframe&quot;)</code></p>
  <p>Neither of those found any iframe elements. I'm seeing the following error:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;myfile.py&quot;, line 60, in &lt;module&gt;
      iframe = driver.find_element_by_xpath(&quot;//iframe[contains(@style, 'display: block')]&quot;)
    File &quot;C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py&quot;, line 394, in find_element_by_xpath
      return self.find_element(by=By.XPATH, value=xpath)
    File &quot;C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py&quot;, line 978, in find_element
      'value': value})['value']
    File &quot;C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py&quot;, line 321, in execute
      self.error_handler.check_response(response)
    File &quot;C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py&quot;, line 242, in check_response
      raise exception_class(message, screen, stacktrace)
  selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression &quot;//iframe[contains(@style, 'display: block')]&quot; is: [object HTMLIFrameElement]. It should be an element.
    (Session info: chrome=93.0.4577.63)
  </code></pre>
  <p>Any thoughts on why the xpath is returning [object HTMLIFrameElement] and why I can't access that as I do other objects when searching by xpath?</p>
  <p><strong>Edit</strong></p>
  <p>New code option 1:</p>
  <pre><code>iframes = driver.find_elements_by_xpath(&quot;.//iframe[contains(@style,'display: block')]&quot;)
  </code></pre>
  <p>This still throws the exact same error as above</p>
  <p>New code option 2:</p>
  <pre><code>parent = driver.find_element_by_xpath(&quot;//div[@class='parent']&quot;)
  iframes = parent.find_elements_by_tag_name(&quot;iframe&quot;)
  // when I print typeof iframes here, it's a list of dicts
  // find the right index. Here, for simplicity, I just set it a default value
  index = 4
  // ...
  driver.switch_to.frame(iframes[index])
  </code></pre>
  <p>I get the following error:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;myfile.py&quot;, line 76, in &lt;module&gt;
      driver.switch_to.frame(iframe)
    File &quot;C:\Python27\lib\site-packages\selenium\webdriver\remote\switch_to.py&quot;, line 89, in frame
      self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference})
    File &quot;C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py&quot;, line 321, in execute
      self.error_handler.check_response(response)
    File &quot;C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py&quot;, line 242, in check_response
      raise exception_class(message, screen, stacktrace)
  selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'ELEMENT'
    (Session info: chrome=93.0.4577.82)
  </code></pre>
  <p>And when I print out iframes:</p>
  <pre><code>[{}, {}, {}, {}, {u'ontouchmove': {}, u'ontouchstart': {}}, {}, {}, {}, {}, {}]
  </code></pre>
  <p>For reference, this is the page I'm trying to hit. Sometimes you have to refresh a few times to get the challenge, and it comes far more frequent when using selenium. Also, using headless mode causes the challenge to happen everytime... <a href="https://catalog.usmint.gov/coins/coin-programs/morgan-and-peace-silver-dollar-coins/" rel="nofollow noreferrer">https://catalog.usmint.gov/coins/coin-programs/morgan-and-peace-silver-dollar-coins/</a></p>