When automating a
CKEditor-based rich text editor using Selenium, simply using sendKeys() often
does not work because the editor content is inside an
iframe, and the text area is usually a contenteditable <body> or <div>.
public class CKEditorExample {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("
https://ckeditor.com/ckeditor-4/demo/"); // or your own app
Thread.sleep(3000); // Wait for editor to load
// 1. Switch to the iframe
WebElement iframe = driver.findElement(By.cssSelector(".cke_wysiwyg_frame"));
driver.switchTo().frame(iframe);
// 2. Locate the editable <body> and enter text
WebElement body = driver.findElement(By.cssSelector("body"));
body.clear(); // Optional: to clear existing content
body.sendKeys("Hello, this is text entered in CKEditor!");
// 3. Switch back to default content if needed
driver.switchTo().defaultContent();
Thread.sleep(3000);
driver.quit();
}
}
If sendKeys() Still Doesn’t Work
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].innerHTML = 'This is inserted using JS';", body);