Selenium and iframe in html Selenium and iframe in html selenium selenium

Selenium and iframe in html


driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) assuming that driver is a healthy instance of webdriver. To continue with the default content do driver.switch_to.default_content()

EDIT: When you have switched to needed frame, locate your webelement as you always do. I guess (but not sure) in your case this will be a html/body, so

el = driver.find_element_by_xpath('html/body')

should be fine. And perform

el.send_keys('keys_to_send')

EDIT2: Before sending keys you may have to focus on the element (click should do the thing, a child element should appear). Or you can just place the needed text via JS.

driver.execute_script('document.body.innerHTML = "%s"' % text_var)


The Accepted answer works very well for the given question. However I have two reasons for adding one more answer and hope this answer might help someone landing this question with different ask.

Reason 1:

Accepted answer has one way of handling iframes inside a web page. However there are different ways to handle iframes.

  • By using index position of the frame
  • By using name of the frame
  • By using id of the frame

Reason 2:

It uses driver.find_element_by_tag_name("iframe") statement to find iframe element which may not work or return expected iframe web element when there are multiple iframe elements in the same page.

There are many articles out there to explain handling iframes in detail.

  1. http://allselenium.info/handling-iframes-using-selenium-webdriver/


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.