Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

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.

I was trying to select an iframe and having no luck (or not repeatably anyway). The HTML looked like this:

<iframe id="upload_file_frame" width="100%" height="465px" frameborder="0" framemargin="0" name="upload_file_frame" src="/blah/import/">

<html>

    <body>

        <div class="import_devices">

            <div class="import_type">

                <a class="secondary_button" href="/blah/blah/?source=blah">

                    <div class="import_choice_image">

                        <img alt="blah" src="/public/images/blah/import/blah.png">

                    </div>

                    <div class="import_choice_text">Blah Blah</div>

                </a>

            </div>

        </div>

    </body>

</html>

The Python code (using the selenium library) was trying to find this iframe using this:

@timed(650)

def test_pedometer(self):

    sel = self.selenium

    ...

    time.sleep(10)

    for i in range(5):

        try:

            if sel.select_frame("css=#upload_file_frame"): break

        except: pass

        time.sleep(10)

    else: self.fail("Cannot find upload_file_frame, the iframe for the device upload image buttons")

Repeated falls with every combination of Selenium commands I could find. 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.

1 Answer

0 votes
by (62.9k points)

You have to perform switching, all you need to do is switch into the frame and then switch back out. Firstly, you would have to locate them on the web page. Take a look at the code; here I have provided three different mechanisms by which you can do this. They are:

– By using the tag name ( here tag name is ‘iframe’)

– By using the Id of IFrame

– By using the name of IFrame

Here is a specific code snippet which will perform switching between frames.

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.by import By

import time

driver = webdriver.Firefox()

driver.maximize_window()

location = "file://<Specify Path to IFrame.HTML>"

driver.get(location)

########Section-1

# we will use tag "iframe" to get the list of iframes present on the web page 

seq = driver.find_elements_by_tag_name('iframe')

print("No of frames present in the web page are: ", len(seq))

#switching between the iframes based on the index

for index in range(len(seq)):

    driver.switch_to_default_content()

    iframe = driver.find_elements_by_tag_name('iframe')[index]

    driver.switch_to.frame(iframe)

    driver.implicitly_wait(30)

    #highlight the contents of the selected iframe

    driver.find_element_by_tag_name('a').send_keys(Keys.CONTROL, 'a')

    time.sleep(2)

    # undo the selection within the iframe

    driver.find_element_by_tag_name('p').click()

 driver.implicitly_wait(30)

driver.switch_to.default_content()

########Section-2

#switch to a specific iframe (First frame) using Id as the locator

iframe = driver.find_element_by_id('FR1')

driver.switch_to.frame(iframe)

time.sleep(2)

driver.find_element_by_id('s').send_keys("Selected")

driver.switch_to.default_content()

########Section-3

#switch to a specific iframe (Second frame) using the name as the locator

iframe = driver.find_element_by_name('frame2')

driver.switch_to.frame(iframe)

time.sleep(2)

driver.find_element_by_tag_name('a').send_keys(Keys.CONTROL, 'a')

If you analyze the code:

1) First of all, you must save the HTML code, given above as IFrame.HTML on your machine.

2) Next, you must provide the correct path in the placeholder given in the above snippet. You must use forward-slash while specifying the file-path of the web page. Otherwise, it may not work accurately. For example, here I have given the path of the file as.

location = "file://C:/Users/Automation-Dev/Desktop/selenium/IFrame.HTML"

3) Section-1 of the code provides the list of IFrames present on the web page.

seq= driver.find_elements_by_tag_name('iframe')

4) We do the switching between the IFrames by traversing through this list using the following step.


iframe = driver.find_elements_by_tag_name('iframe')[index]

driver.switch_to.frame(iframe)

5) Every time you need to move back from an IFrame to the parent HTML. Selenium Webdriver provides the following method.

driver.switch_to.default_content()

6) In Section-2, we switch to a specific IFrame using the locator as ‘id.’

iframe = driver.find_element_by_id('FR1')

7) In Section-3, we switch to a specific IFrame using the locator as ‘name.’

iframe = driver.find_element_by_name('frame2')

I hope this helps!

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s automation testing course online!

Browse Categories

...