Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't sendKeys() to TinyMCE with Selenium WebDriver

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();
like image 342
Sean Avatar asked Feb 11 '14 21:02

Sean


2 Answers

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

like image 144
Yi Zeng Avatar answered Oct 04 '22 20:10

Yi Zeng


Do this (c#)

yourWebDriver.ExecuteScript(string.Format("tinyMCE.getInstanceById('{0}').setContent('{1}');",yourWebElement.GetAttribute("id"), "hello world"));
like image 41
kernowcode Avatar answered Oct 04 '22 20:10

kernowcode