A pragmatic approach to QA and OPS

Handling Iframes In A Webpage

Iframes are another one of the HTML elements, which present an interesting scenario, while automating with Selenium. An iframe, is nothing but an HTML element embedded inside an HTML document.

iframes are recognized by a separate < iframe > < /iframe > tags inside an HTML document.

We saw that in our example, while handling drag and drop scenarios, we use a switch_to.frame( ) method to jump to a frame.

Selenium Python API gives us the switch_to.frame (self, frame_reference) method to switch to a frame.

driver.switch_to.iframe(self,frame reference)

The frame_reference parameter can be any locator mechanism,by which we can recognize the iframe.

Take a look at this page, there are two iframes, which are embedded in this web page.

Screenshot from 2015-10-04 21:42:19


If you look at the HTML here, we can use three different methods to recognize the iframe-

  • use the tag name (iframe)
  • use the ID of iframe
  • use the name of iframe

Let’s use the ID attribute to switch to the iframe, and enter an email inside the email box using the send_keys( ) method.


iframe1 = driver.find_element_by_id('IF1')


Now, once you are done performing what ever you want to do inside your frame, you need to go back to the original HTML content of the page. Selenium gives us this ability by the switch_to.default_content( ) method.



If you want to see how many iframes are present in the web page, it’s a good idea to use the power of Python lists to find that.

We will use the find_elements_by_tag_name( ) method to collect all the elements with the tag < iframe > and then use the len( ) method to find the length of the list.

list = driver.find_elements_by_tag_name('iframe')


Here is the code :


__author__ = 'rahul'

import unittest
from selenium import webdriver

class Iframe(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_Iframe(self):
        driver = self.driver

        iframe1 = driver.find_element_by_id('IF1')



        list = driver.find_elements_by_tag_name('iframe')


    def tearDown(self):

if __name__ == '__main__':

Interacting with elements inside iframe, is not an easy and straight forward task. We will see that in out future posts.

%d bloggers like this: