Selenium and iframe in html

You can switch to the iframe using multiple approaches as per the prevailing condition of the HTML DOM.

  • If the <iframe> is the only iframe within the DOM Tree you can use the following line of code:

    • Using TAG_NAME:

      driver.switch_to.frame(driver.find_element(By.TAG_NAME, "iframe"))
      
  • Locating the <iframe> element with a unique Locator Strategy:

    • Using css_selector:

      driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, "iframe.textarea[src='/framework/html/blank.html']"))
      
    • Using xpath:

      driver.switch_to.frame(driver.find_element(By.XPATH, "//iframe[@class='textarea' and @src='/framework/html/blank.html']"))
      

Ideally to switch to an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following Locator Strategies:

    • Using TAG_NAME (only iframe present):

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.TAG_NAME, "iframe"))).click()
      
    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.textarea[src='/framework/html/blank.html']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "element_css_selecor"))).send_keys("character_sequence")
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='textarea' and @src='/framework/html/blank.html']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='TlogBtn']/span[@class='mailicn']/img"))).send_keys("character_sequence")
      
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

Reference

You can find a couple of relevant discussions in:

  • Switch to an iframe through Selenium and python
  • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
  • selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

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/