Select iframe using Python + Selenium

python selenium find all iframes
python selenium switch to iframe by id
switch to frame in selenium python
iframe selenium c#
document in iframe selenium
selenium find element in iframe
python selenium select frame by index
how to switch from child iframe to parent frame in 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_button" href="/blah/blah/?source=blah">
                    <div class="import_choice_image">
                        <img alt="blah" src="/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.

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) ##

Select iframe using Python + Selenium, 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  Switch Between IFrames Using Selenium Python 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. 3) In Section-1 of the code, 4) We do the switching between the IFrames

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:


Switch Between IFrames Using Selenium with Python, Switch Between IFrames Using Selenium Python. If a webpage contains multiple IFrames, then we will need to switch between them. Selenium Python API provides “switch_to. iframe (self, frame_reference)” method to move to a particular IFrame. – 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 import By. import time

What finally worked for me was:


Basically, don't use selenium to find the link in the iframe and click on it; use jQuery. Selenium has the capability to run an arbitrary piece of javascript apparently (this is python-selenium, I am guessing the original selenium command is runScript or something), and once I can use jQuery I can do something like this: Selecting a form which is in an iframe using jQuery

Select iframe using Python + Selenium, 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  In order to handle iframes on a web page you need to switch to the iframes first that are present on the page using this command: driver.switch_to.frame(frame_id) This tutorial has a working example of handling iframe using selenium with python which will made you able to get through this issue.

You don't need to use JavascriptExecutor. All you needed to do was switch into the frame and then switch back out, like so:

// do stuff on main window
// then do stuff in the frame
// then do stuff on main window again

As long as you are careful with this, you will never have a problem. The only time I always use a JavascriptExecutor is to get window focus since I think using Javascript is more reliable in that case.

Handling iFrames in Selenium Webdriver: switchTo(), Selenium Python API gives us the switch_to.frame (self, frame_reference) method to switch to a frame. 1. driver.switch_to  Selenium (Python) - SELECT. Element may have been inside an iframe or another context which was refreshed. Drop-down selection using XPath Selenium Python. 0.

Selenium's selectFrame command accepts all the standard locators like css=, but it also has a an extra set of locators that work specifically with FRAME and IFRAME elements.

As the doc says:

selectFrame ( locator ) Selects a frame within the current window. (You may invoke this command multiple times to select nested frames.) To select the parent frame, use "relative=parent" as a locator; to select the top frame, use "relative=top". You can also select a frame by its 0-based index number; select the first frame with "index=0", or the third frame with "index=2".

You may also use a DOM expression to identify the frame you want directly, like this: dom=frames["main"].frames["subframe"]

Arguments: locator - an element locator identifying a frame or iframe

In general, you'll have better luck using the specialized locators, especially if you establish the right context first (e.g., select_frame("relative=top"); select_frame("id=upload_file_frame");).

How to handle iframes using Selenium WebDriver, I want to select an iframe and I am not able to do it now. Below is the HTML where I want or something? Can someone lead me in the right  I was able to select an "src" frame with no name or ID using this method & Python Selenium. I found the xpath of the element, and did this code to get selenium to select the frame properly (using Python 2.7):

Handling Iframes In A Webpage – All Things Selenium and QA, Selenium Webdriver allows users to interact with iframes on the page but This tutorial is in Duration: 7:32 Posted: Jan 13, 2018 IFrame is a web page which is embedded in another web page or an HTML document embedded inside another HTML document. The IFrame is often used to insert content from another source, such as an advertisement, into a Web page. The <iframe> tag specifies an inline frame.

Need to select an IFrame with selenium (python), Visit our blogs for more Tutorials & Online training Selenium with Python Tutorial 13-How to Duration: 12:18 Posted: Oct 18, 2018 The best way to use class to work to with dropdown selection but some time it does not work as expected due to designing issue or other issues of the HTML. In this type of situation you can also prefer as alternate solution using execute_script() as below :-

Selenium Webdriver - iFrames, How to identify the iframe: How do switch over the elements in iframes using Web Driver commands: Concept of Nested Frames (Frames inside  Recent in Selenium. Using the same in IE browser but getting half the screen as black and half is coming 19 hours ago; Getting different texts from the HTML DOM through Selenium and Python or 22 hours ago; How to write a code using selenium webdriver and pom, to get 5 mobiles from a popular online store whose price is less than 30000? 2

  • Somehow the WebDriver finds the elements into the iframe when searching with xpath. I've tried finding elements by CSS, ID but it always gave me an error of not existing element (timeout failure actually because I used the waitForElement command). I didn't think searching element by xpath would make the difference, but I fortunately gave it a try when I was running out of ideas. Thanks a lot!
  • You'd be better off using Selenium's getEval command (probably "get_eval" in the Python bindings) instead of its runScript command. getEval won't return until the JavaScript code completes, whereas runScript simple inserts a <SCRIPT> element and returns, resulting in the browser running the JavaScript code after the call returns.
  • what exactly is sel.run_script? I can find no mention of this function in the docs or with google. While this may have worked for you, its a poor answer because you dont explain what self.selenium is or how to get run_script That said, driver.execute_script worked for me
  • Well, to be honest, it was five years ago, I have no idea by now. Glad that driver.execute_script worked for you, though, good that you posted that here for the future readers.
  • Yeah, selectFrame didn't work for me. Doesn't mean it won't work for anyone, but I found it to actually select the frame about one time in 5.