5

I need to type data into an iframe. I referred to

Typing in a IFrame with Selenium IDE

but selenium.selectFrame(<xpath>) returns: Element not found error and no css has been defined for the iframe.

Using firebug:

<iframe frameborder="0" allowtransparency="true" tabindex="0" src="" title="Rich text editor, templateWizardCKEditor1, press ALT 0 for help." style="width: 100%; height: 100%;"/>

What could be a solution for this?

Community
  • 1
  • 1
aradhak
  • 836
  • 1
  • 9
  • 22

3 Answers3

7

I found a solution for the same...

driver.switchTo().frame("ext-gen298");
WebElement editable = driver.switchTo().activeElement();
editable.sendKeys("Your text here");
driver.switchTo().defaultContent();

Ref: http://code.google.com/p/seleniumwikiFrequentlyAskedQuestions#Q:_How_do_I_type_into_a_contentEditable_iframe?

Botz3000
  • 39,020
  • 8
  • 103
  • 127
Somesh
  • 71
  • 1
  • 1
3

That just means you used some bad xpath.

selenium.selectFrame("xpath=//iframe[contains(@title,'Rich text editor')]");

This should work. It selects the iframe based on an xpath expression that looks for an iframe which title attribute contains "Rich text editor".

For more xpaths, see XPath v1.0 on w3.org and XPath v2.0 on w3.org - only for some browsers.

By the way, the iframe can be selected by css selectors, too, even if it has no css assigned. The selector can select any element based on its position in tree hierarchy and it's attributes - similar to XPath. To learn about css selector, try The w3 again or wikipedia

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • 1
    selenium.selectFrame("xpath=//iframe[contains(@title,'Rich text editor')]"); selenium.type("xpath=//iframe[contains(@title,'Rich text editor')]", tag); But got Element not found error. So I tried selenium.selectFrame("xpath=//iframe[contains(@title,'Rich text editor, templateWizardCKEditor3, press ALT 0 for help.')]"); selenium.type("xpath=//iframe[contains(@title,'Rich text editor, templateWizardCKEditor3, press ALT 0 for help.')]", tag); but of no use. – aradhak Mar 29 '12 at 07:16
  • Well if you want to write right into the iframe, that could be a problem. After the first `selectFrame()`, you can try [this method](http://selenium.googlecode.com/svn/trunk/docs/api/java/com/thoughtworks/selenium/Selenium.html#getWhetherThisFrameMatchFrameExpression%28java.lang.String,%20java.lang.String%29). Writing into editable iframes is tricky and can be done [with WebDriver](http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_How_do_I_type_into_a_contentEditable_iframe?) or by some JavaScript similar to the one shown there, I guess. – Petr Janeček Mar 29 '12 at 08:52
  • ...because when you do `selectFrame()`, you basically have that frame as your new root node. Meaning that if you want to write into the frame, you would need to call `selenium.type("xpath=/", tag);` ... and that might actually work, too! Give it a try :) – Petr Janeček Mar 29 '12 at 08:54
  • Huh! I'd like to get my hands on an reproducible example. If your page is proprietary, could you try to create one? Or what exactly is the `iframe` content? Other than that ... try the [Selenium Google Groups](https://groups.google.com/forum/?fromgroups#!forum/selenium-users). – Petr Janeček Jun 11 '12 at 06:54
0
  1. Select the frame first,
  2. Then click it to separate the header information (WSYWG) from the text area,
  3. Then focus on the text area (tinymce)
  4. Then send your text to populate the field (we have 2 examples to show different objects that work)
  5. Then select Window back to default window (null)

Here is what I used that worked:

<td>selectFrame</td>
<td>xpath=//*[contains (@id, 'mce_0_ifr')]</td>
<td></td>

<td>click</td>
<td>xpath=//*[contains (@id, 'tinymce')]</td>
<td></td>

<td>focus</td>
<td>xpath=//*[contains (@id, 'tinymce')]</td>
<td></td>

<td>sendKeys</td>
<td>xpath=//*[contains (@id, 'tinymce')]</td>
<td>I Typed in an iFrame!!!</td>

<td>sendKeys</td>
<td>css=body#tinymce.mceContentBody</td>
<td>0</td>

<td>selectWindow</td>
<td>null</td>
<td></td>
vegemite4me
  • 6,621
  • 5
  • 53
  • 79