Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 is:

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

like image 984
rossdavidh Avatar asked Sep 23 '11 20:09

rossdavidh


People also ask

How do I click iframe in Selenium?

Right click on the element, If you find the option like 'This Frame' then it is an iframe. (Please refer the above diagram) Right click on the page and click 'View Page Source' and Search with the 'iframe', if you can find any tag name with the 'iframe' then it is meaning to say the page consisting an iframe.


1 Answers

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 ## time.sleep(3) ## You have to switch to the iframe like so: ## driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) ## 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) ## driver.switch_to.default_content() 
like image 56
Michael Whitman Avatar answered Sep 23 '22 16:09

Michael Whitman