type into iframe in selenium

2019-05-09 10:15发布

问题:

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?

回答1:

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?



回答2:

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



回答3:

  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>