Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I access a dynamically added iframe using Selenium WebDriver?
    text
    copied!<p>I have a page that starts off without an iframe, and then via JavaScript gets an iframe added after clicking an anchor.</p> <p>The problem I'm having is that when switching to the frame with <code>driver.switch_to_frame(x)</code> I still can find any of my content.</p> <p>I've tried looping through the frames found with <code>driver.find_elements_by_tag_name('iframe')</code> and checking each of them for the class I expect to find, but no such luck.</p> <p><code>driver.switch_to_active_element()</code> does not give me the correct iframe either.</p> <p>I'm not sure if the iframe content is just not accessible due to the JS DOM changes not reflected in what Selenium is seeing from the driver. I've gone through the same process for other iframes with no problems, but this one just won't cooperate. Below is a simplified version of what I'm dealing with.</p> <p>Before JS:</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;a onclick="jsmagic"&gt;load iframe&lt;/a&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>After JS:</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;iframe&gt;&lt;div class='modal'&gt;Message&lt;/div&gt;&lt;/iframe&gt; &lt;a onclick="jsmagic"&gt;load iframe&lt;/a&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Python WebDriver attempt:</p> <pre><code>driver.switch_to_frame(0) driver.find_elements_by_class_name('modal') driver.switch_to_default_content() </code></pre> <p>I've also tried variations on that like:</p> <pre><code>frame = driver.find_elements_by_tag_name('iframe')[0] driver.switch_to_frame(frame) driver.find_elements_by_class_name('modal') </code></pre> <p>I've event tried using <code>driver.execute_script</code> to get to the object, but getting at the content is beyond me. The Firefox console commands that work doesn't run via Selenium.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload