selecting an iframe using python selenium

0
0

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

  • You must to post comments
0
0

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()
  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website