Selenium and iframe in html
Asked Answered
M

4

31

Unable to use send_keys() on an element inside an iframe. How can I select this iframe, and which element inside should be used for send_keys()?

page image

Here's the html code of the iframe:

<iframe class="textarea" src="/framework/html/blank.html" style="width: 99%; border-width: 1px; height: 332px;">
#document
<html webdriver="true">
<head>
</head>
<body> … </body>
</html>
</iframe>

How can I send text to the Description area?

And how can I view the page source of the iframe if it doesn't appear when using "View Page Source" in the browser?

Marbut answered 20/9, 2013 at 19:9 Comment(0)
P
72

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)

Pleader answered 20/9, 2013 at 19:13 Comment(4)
sorry i am a newbie. but driver.switch_to_frame(driver.find_element_by_tag_name("iframe")) how to use this thing to send key to description windows? like now driver has switched to frameMarbut
@Marbut Edited an answer as you requestedPleader
element <body>got select, the send_keys() is also executing without error but value is not coming on the page.Marbut
Hey there, was using this answer as a reference. You use driver.switch_to_frame, but the documentation on their site uses driver.switch_to.frame. Which one is correct?Pathos
B
3

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/
Birddog answered 19/1, 2019 at 15:1 Comment(0)
L
1

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:

Leipzig answered 15/12, 2021 at 15:46 Comment(0)
B
0

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.

Barbet answered 10/12, 2016 at 19:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.