Question

I am using Selenium WebDriver in Eclipse and I am trying to insert text into a tinymce box on a web page. This is the code I am using.

//find tinymce iframe
editorFrame = driver.findElement(By.cssSelector(".col_right iframe"));

//switch to iframe
driver.switchTo().frame(editorFrame);
driver.findElement(By.className("mceContentBody")).sendKeys("YOOOO");
driver.switchTo().defaultContent();

The cursor is blinking inside of the editor, however no text is sent. I have also tried this slight variation without any luck.

//find tinymce iframe
editorFrame = driver.findElement(By.cssSelector(".col_right iframe"));  

//switch to iframe
driver.switchTo().frame(editorFrame);
WebElement body = driver.findElement(By.className("mceContentBody"));
body.sendKeys("YOOOO");
driver.switchTo().defaultContent();
Was it helpful?

Solution

Yes, as what Richard says, this is a duplicate of How to input text into tinceMCE editior using selenium/webdriver.

For your specific code, I'd suggest

  • Try different locator for mceContentBody, e.g use By.cssSelector(".mceContentBody"), By.cssSelector("body"), etc.

  • Click the body first before sending keys.

driver.findElement(By.tagName("body")).click().sendKeys("YOOOO");
  • Set innerHTML
inputWebDriver.switchTo().frame("input-data_ifr");
WebElement element = inputWebDriver.findElement(By.cssSelector("body"));
(JavascriptExecutor)driver.executeScript("arguments[0].innerHTML = '<h1>Set text using innerHTML</h1>'", element);
  • Use TinyMCE's native API
// no need to switch iframe
(JavascriptExecutor)driver.executeScript("tinyMCE.activeEditor.setContent('<h1>Native API text</h1> TinyMCE')");

Further reading: Test WYSIWYG editors using Selenium WebDriver

OTHER TIPS

Do this (c#)

yourWebDriver.ExecuteScript(string.Format("tinyMCE.getInstanceById('{0}').setContent('{1}');",yourWebElement.GetAttribute("id"), "hello world"));

Snippet for writing to the tinymce editor:

Note - I will not suggest returning void for writing to editor method but keeping it here for simplicity -

public void writeTextToEditor(String text) {
   WebElement iframe = 
   driver().findElements(By.tagName(UtilTexts.HTML_ELEMENT_IFRAME)).get(0);

   driver().switchTo().frame(iframe);// something to look for when using timymce
   txt_editorBoxSupportArticle.click(); // if the focus is not on the editor window by default you have to perform click.
   txt_editorBoxSupportArticle.sendKeys(text);

   getDriverForCurrentThread().switchTo().defaultContent();
}

Snippet for reading from the tinymce editor:

public String readTextFromEditor() {
    @FindBy(how = How.ID, using = ("tinymce"))
    private WebElement txt_editorBoxSupportArticle;

    WebElement iframe = 
    driver().findElements(By.tagName(UtilTexts.HTML_ELEMENT_IFRAME)).get(0);

    driver().switchTo().frame(iframe);
    String articleDesc = txt_editorBoxSupportArticle.getText();
    getDriverForCurrentThread().switchTo().defaultContent();
    return articleDesc;
}

Code snippets above are tested. Although I will also suggest trying TinyMCE's native API.

Please share your thoughts if you find it useful.

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top