Select iframe using Python + Selenium

Posted on

Solving problem is about exposing yourself to as many situations as possible like Select iframe using Python + Selenium and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about Select iframe using Python + Selenium, which can be followed any time. Take easy to follow this discuss.

Select iframe using Python + Selenium

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/">
        <div class="import_devices">
            <div class="import_type">
                <a class="secondary_buttonhref="/blah/blah/?source=blah">
                    <div class="import_choice_image">
                        <img alt="blahsrc="/public/images/blah/import/blah.png">
                    <div class="import_choice_text">Blah Blah</div>

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

def test_pedometer(self):
    sel = self.selenium
    for i in range(5):
            if sel.select_frame("css=#upload_file_frame"): break
        except: pass
    else:"Cannot find upload_file_frame, the iframe for the device upload image buttons")

Repeated fails 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.

Answer #1:

This worked for me with Python (v. 2.7), webdriver & Selenium when testing with iframes and trying to insert data within an iframe:

self.driver = webdriver.Firefox()
## Give time for iframe to load ##
## You have to switch to the iframe like so: ##
## Insert text via xpath ##
elem = driver.find_element_by_xpath("/html/body/p")
elem.send_keys("Lorem Ipsum")
## Switch back to the "default content" (that is, out of the iframes) ##
Answered By: Michael Whitman

Answer #2:

If iframe is dynamic node, it’s also possible to wait for iframe appearence explicitly and then switch to it using ExpectedConditions:

from import expected_conditions as EC
from import WebDriverWait as wait
driver = webdriver.Chrome()
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("iframe_name_or_id"))

If iframe doesn’t have @id or @name it can be found as common WebElement using driver.find_element_by_xpath(), driver.find_element_by_tag_name(), etc..:

wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_xpath("//iframe[@class='iframe_class']")))

To switch back from iframe:

Answered By: Andersson

Leave a Reply

Your email address will not be published. Required fields are marked *