Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium and iframe in html

Unable to use send_keys() for an iframe. How to select this iframe and which element inside this should be use for send_key()?

page image

and iframe html code

<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 to send the value to description?

One more thing i want to know that this code of frame is not coming when i go for "view page source " in browser?

like image 588
RATHI Avatar asked Sep 20 '13 19:09

RATHI


2 Answers

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)

like image 77
Furious Duck Avatar answered Sep 25 '22 17:09

Furious Duck


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/
like image 31
arunkvelu Avatar answered Sep 23 '22 17:09

arunkvelu