1

I went through a few posts on SO and used the suggested approach of switching to the iframe -

driver.switch_to.frame(driver.find_element_by_id(iFrameID))
element = driver.find_element_by_xpath('HTML/BODY')

However, I get a NoSuchElementException for the find by xpath.

I then added a print (driver.page_source) after switching to the iframe, and saw the following structure -

<HTML dir=ltr><HEAD><LINK rel=stylesheet type=text/css 
href="/_layouts/1033/styles/core.css">
<META name=GENERATOR content="MSHTML 11.00.9600.18639"></HEAD>
<BODY class=ms-formbody contentEditable=true style="BORDER-TOP: medium none; 
BORDER-RIGHT: medium none; BORDER-BOTTOM: medium none; BORDER-LEFT: medium 
none; BACKGROUND-COLOR: white" scroll=yes WebLocale="1033"
BaseElementID="baseTextField" wordWrap="false" RestrictedMode="true">
<DIV></DIV></BODY></HTML>

The HTML/BODY structure is definitely present so I'm not sure what I'm doing wrong in the find_element_by_xpath. I also tried /HTML/BODY and//HTML/BODY with no luck.

As a workaround, I tried to bring the RTE into focus by clicking the iframe and then using ActionChains

driver.find_element_by_id(iFrameID).click()
actions = ActionChains(driver)
actions.send_keys("Lorem Ipsum")
actions.perform()

But got the following error:

NameError: name ActionChains is not defined

Any help would be appreciated

6
  • I might be able to help, if you can post your source code :) Commented Jun 18, 2017 at 17:41
  • it's obvious that you are missing import, try from selenium.webdriver import ActionChains Commented Jun 18, 2017 at 18:13
  • Can you actually copy-paste the HTML of the <iframe> tag you are trying to switch to? The output of driver.page_source has been truncated. Stop relying on that method... it's inconsistent and doesn't do what you think it does. @AzatIbrakov he is trying to switch to an iframe, he first encountered the lib import as you pointed out. But, he still will get the NoSuchElementException error for the driver.find_element_by_id(iFrameID).click() command. Commented Jun 18, 2017 at 18:17
  • @Sid Can you consider updating the HTML DOM of the iframe you are trying to switch? Do update what is your next step after switching to the iframe. Thanks Commented Jun 18, 2017 at 18:35
  • @Sid, why are you trying to click the iframe and sent text to ActionChains() instance!? Share what exactly you want to do as for now your logic is quite unclear Commented Jun 18, 2017 at 19:44

1 Answer 1

2

This post is a little old, but I hope my solution is able to help anyone out there that is facing similar issues.

So the way I managed to send keys into an IFrame using Selenium Python is by first using switch_to_frame of iframe after switching to the iframe, we can query the document like new document using driver.find_element

Sample shown below

#Select iFrame
element = driver.find_element_by_id(iFrameID)

#Switch to iFrame
driver.switch_to_frame(element)

#Query document for the ID that you're looking for
queryElement = driver.find_element_by_id(queryID)

#Send key to the ID
queryElement.send_keys("L")

#Switch back to default content from iFrame
driver.switch_to_default_content()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.