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();
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
, eg use By.cssSelector(".mceContentBody")
, By.cssSelector("body")
, etc.
Click the body first before sending keys.
driver.findElement(By.tagName("body")).click().sendKeys("YOOOO");
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);
// 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
这样做(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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.